开发者

How to check if a generic type instance is 'inherited' from another one, both generic type and the argument type

I'd like to check so开发者_C百科mething like below:

  List<string> lst=new List<string>();
if(lst ?? is "Inherited" from "IEnumerable<object>" ??)

I mean checking whether both the 'wrapper' class and the argument class are inherited from the given classes.

Any suggest will be appreciated!


It's not really clear what you mean. You can check whether it's assignable to a type very easily:

List<string> list = new List<string>();
if (list is IEnumerable<object>) // True for .NET 4, false for earlier versions
{
    ...
}

In this case we're relying on the generic covariance of IEnumerable<T>. Not all interfaces support that, and classes certainly don't.

If you could give us more indication of the bigger picture - what you're trying to achieve - that would really help.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜