开发者

How to cast some object to generic class of parent of T?

开发者 https://www.devze.com 2022-12-14 02:48 出处:网络
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.

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(at开发者_StackOverflowtribute.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,


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号