tags:

views:

274

answers:

3

The main problem of this question is when we pass T into some function and use it to cast some object like the following statement.

void SomeFunction<T> (object model)
{
    (SomeClass<T>)model;
}

Everything works fine. But I want to cast model object to generic class object that is parent of T or grand parent of T depend on what is not empty. How to do that?

Updated # 1

For more understanding, please look at the following example.

public partial class SimpleUserInfo
{
    public string LogOnName { get;set; }
    public string HashedPassword { get;set; }
}

public partial class UserInfo : SimpleUserInfo
{
    pubic string Address { get;set; }
}

After I create some data models. I create some generic class that use UserInfo class as parameter.

public class SimpleUserInfoValidator : IValidator<SimpleUserInfo>
{
    // logic to validate simple user info instance
}

And then, I add attribute to SimpleUserInfo class.

[Validator(typeof(SimpleUserInfoValidator))]
public partial class SimpleUserInfo {}

Finally, I create some function for retrieving validator in given class T.

public GetValidator<T> ()
{
    var attribute = (ValidatorAttribute)Attribute.GetCustomAttribute(type, typeof(ValidatorAttribute));

    if (attribute == null || attribute.ValidatorType == null)
        return null;

    var  (IValidator<T>)Activator.CreateInstance(attribute.ValidatorType);   
}

This function will works fine when T is SimpleUserInfo but problem will occur when T is UserInfo. How to solve this?

PS. To solve this question does not require to use new feature of C# 4.0. But I just tell you about I will apply this solution in C# 4.0.

Thanks,

+2  A: 

I assume that you are looking for covariance, but that feature (new to C# 4) is only available for interfaces, not for classes.

Edit: According to your edit, you're meaning co-/contravariance. However, if and how the interface IValidator<T> can be made covariant (which seems to be what you're looking for) depends on the methods and properties of the IValidator<T> interface.

Lucero
A: 

It sounds like you'll need to constrain the type of T to some Interface that you create:

void SomeFunction<T> (object model) where T : IHasParent
{
    var parent = ((T)model).GetParent();
}

public interface IHasParent
{
    object GetParent();
}

This is called a generic constraint.

I'd guess you should be able to do this too:

void SomeFunction<T> (T model) where T : IHasParent
{
    var parent = model.GetParent();
}

public interface IHasParent
{
    IHasParent GetParent();
}

I hope this gives you some ideas to take forwards, let us know what works for you.

You could handle nulls in numerous ways, I'll leave that as an exercise for you. Have a search for the Null Coalescing Operator for example.

rohancragg
It does not my point. Please read my updated question.
Soul_Master
I believe OP didn't exactly mean "parent class", but rather "base class" in inheritance hierarchy.
Groo
+1  A: 

I haven't installed .NET 4.0 yet, so I am not sure about using covariance properly, but you can get covariance support even in .Net 2.0 and 3.5 using a Duck Typing library (e.g. duck typing library by David Meyer or LinFu by Philip Laureano:

In other words, last line in GetValidator<T> should look like this:

// http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx
return DuckTyping.Cast<IValidator<T>>
     Activator.CreateInstance(attribute.ValidatorType);

or (using LinFu)

// http://www.codeproject.com/KB/cs/LinFuPart2.aspx
DynamicObject dynamicObj = new DynamicObject
    (Activator.CreateInstance(attribute.ValidatorType));
return dynamicObj.CreateDuck<IValidator<T>>()

[Edit]

It is possible that I have not understood your question, but I believe it boils down to this:

You have a generic interface with a generic parameter of type T:

IValidator<SimpleUserInfo> simpleUserValidator;

You want to cast it to the same generic interface, but with a generic parameter which is a base class of T:

IValidator<SimpleUserInfo> ---> IValidator<UserInfo>

Simple casting will not work, because generic type covariance is not supported (at least not in older versions of .Net):

// this will throw an invalid cast exception
IValidator<UserInfo> userValidator = (IValidator<UserInfo>) simpleUserValidator;

But it will work using duck typing:

IValidator<UserInfo> userValidator = 
    DuckTyping.Cast<IValidator<UserInfo>> (simpleUserValidator);

Once again, .Net 4.0 should handle covariance, but I haven't tested it. This example will work in any .Net version, so I included it for completeness sake.

Groo
I think you miss understand my question. The main problem of my question is how to create new object that is a parent of T. Please read my updated question #1.
Soul_Master
I think your updated answer is so clear for solving problem and being an accepted answer. By the way, in my real problem I use another way to this question that has less complexity than this answer.
Soul_Master