对'open'泛型类型使用IsAssignableFrom [英] Using IsAssignableFrom with 'open' generic types

查看:450
本文介绍了对'open'泛型类型使用IsAssignableFrom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用反射,我试图找到从给定基类继承的一组类型。找到简单的类型并不需要很长时间,但是当涉及到泛型时我很难。



对于这段代码,第一个IsAssignableFrom返回true ,但第二个返回false。

  class class1 {} 
class class2:class1 {}
class generic1< T> {}
类generic2< T> :generic1< T> {}

类程序
{
static void Main(string [] args)
{
Type c1 = typeof(class1);
类型c2 = typeof(class2);
Console.WriteLine(c1.IsAssignableFrom(c2):{0},c1.IsAssignableFrom(c2));

键入g1 = typeof(generic1<>);
键入g2 = typeof(generic2<>);
Console.WriteLine(g1.IsAssignableFrom(g2):{0},g1.IsAssignableFrom(g2));

generic1< class1> cc = new generic2< class1>();






$ b

那么在运行时如何确定是否有一个通用的类型定义是从另一个派生的?

解决方案

回答另一个问题

  public static bool IsAssignableToGenericType(Type givenType,type genericType)
{
var interfaceTypes = givenType.GetInterfaces();

foreach(var it in interfaceTypes)
{
if(it.IsGenericType& it.GetGenericTypeDefinition()== genericType)
return true;
}

if(givenType.IsGenericType&& givenType.GetGenericTypeDefinition()== genericType)
return true;

类型baseType = givenType.BaseType;
if(baseType == null)return false;

返回IsAssignableToGenericType(baseType,genericType);





$ b如果你喜欢这个答案,请点击上面链接的答案,因为代码是'我的。)


Using reflection, I'm attempting to find the set of types which inherit from a given base class. It didn't take long to figure out for simple types, but I'm stumped when it comes to generics.

For this piece of code, the first IsAssignableFrom returns true, but the second returns false. And yet, the final assignment compiles just fine.

class class1 { }
class class2 : class1 { }
class generic1<T> { }
class generic2<T> : generic1<T> { }

class Program
{
    static void Main(string[] args)
    {
        Type c1 = typeof(class1);
        Type c2 = typeof(class2);
        Console.WriteLine("c1.IsAssignableFrom(c2): {0}", c1.IsAssignableFrom(c2));

        Type g1 = typeof(generic1<>);
        Type g2 = typeof(generic2<>);
        Console.WriteLine("g1.IsAssignableFrom(g2): {0}", g1.IsAssignableFrom(g2));

        generic1<class1> cc = new generic2<class1>();
    }
}

So how do I determine at run time whether one generic type definition is derived from another?

解决方案

From the answer to another question:

public static bool IsAssignableToGenericType(Type givenType, Type genericType)
{
    var interfaceTypes = givenType.GetInterfaces();

    foreach (var it in interfaceTypes)
    {
        if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
            return true;
    }

    if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
        return true;

    Type baseType = givenType.BaseType;
    if (baseType == null) return false;

    return IsAssignableToGenericType(baseType, genericType);
}

(If you like the answer please upvote the linked answer since the code isn’t mine.)

这篇关于对'open'泛型类型使用IsAssignableFrom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆