Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何测试类的实例是否为特定泛型类型?_C#_.net_Generics_Reflection - Fatal编程技术网

C# 如何测试类的实例是否为特定泛型类型?

C# 如何测试类的实例是否为特定泛型类型?,c#,.net,generics,reflection,C#,.net,Generics,Reflection,假设我有一个简单的泛型类,如下所示 public class MyGenericClass<t> { public T {get;set;} } 但我不能只引用MyGenericClass。Visual studio总是希望我编写MyGenericClass来测试您的实例是否属于MyGenericClass的类型,您可以编写类似的内容 MyGenericClass<string> myClass = new MyGenericClass<string>

假设我有一个简单的泛型类,如下所示

public class MyGenericClass<t>
{
   public T {get;set;}
}

但我不能只引用
MyGenericClass
。Visual studio总是希望我编写
MyGenericClass

来测试您的实例是否属于
MyGenericClass
的类型,您可以编写类似的内容

MyGenericClass<string> myClass = new MyGenericClass<string>();
bool b = myClass.GetType().GetGenericTypeDefinition() == typeof(MyGenericClass<>);
编辑:亚当·罗宾逊在评论中提出了一个很好的观点,比如你有
类Foo:MyGenericClass
。上面的测试代码不会将Foo的实例识别为
MyGenericClass
,但是您仍然可以编写代码来测试它

Func<object, bool> isMyGenericClassInstance = obj =>
    {
        if (obj == null)
            return false; // otherwise will get NullReferenceException

        Type t = obj.GetType().BaseType;
        if (t != null)
        {
            if (t.IsGenericType)
                return t.GetGenericTypeDefinition() == typeof(MyGenericClass<>);
        }

        return false;
    };

bool willBeTrue = isMyGenericClassInstance(new Foo());
bool willBeFalse = isMyGenericClassInstance("foo");
Func isMyGenericClassInstance=obj=>
{
if(obj==null)
返回false;//否则将获取NullReferenceException
类型t=obj.GetType().BaseType;
如果(t!=null)
{
if(t.IsGenericType)
返回t.GetGenericTypeDefinition()==typeof(MyGenericClass);
}
返回false;
};
bool-willBeTrue=ismygenericsclassinstance(new-Foo());
bool willBeFalse=isMyGenericClassInstance(“foo”);

如果需要,可以让泛型类实现一些任意(可能是空的)接口。测试某个对象是否属于一般泛型类只需测试它是否实现了该接口。无需显式使用反射。

我使用以下方法(因为我发现所有建议都不适合我)

  public static bool IsGenericTypeOf(Type vtype, Type target) {
     return
           vtype.IsGenericType
        && target.ContainsGenericParameters
        && vtype.Name == target.Name
        && vtype.Assembly == target.Assembly
        && vtype.Module == target.Module
        ;
  }

请注意,如果该类是从该类的泛型形式派生的,则这将不起作用。换句话说,
公共类Foo:MyGenericClass{}
将不合格。@Adam,说得好。您可以编写代码来进一步进行测试。我将添加一个如何做到这一点的想法。
Func<object, bool> isMyGenericClassInstance = obj =>
    {
        if (obj == null)
            return false; // otherwise will get NullReferenceException

        Type t = obj.GetType().BaseType;
        if (t != null)
        {
            if (t.IsGenericType)
                return t.GetGenericTypeDefinition() == typeof(MyGenericClass<>);
        }

        return false;
    };

bool willBeTrue = isMyGenericClassInstance(new Foo());
bool willBeFalse = isMyGenericClassInstance("foo");
List<int> testInt = new List<int>();
List<string> testString = new List<string>();

if (testInt .GetType().Equals(testString.GetType()))
 Console.WriteLine("Y");
else Console.WriteLine("N");
testInt.GetType().Equals(typeof(List<int>))
is true
testInt.GetType().FullName
  public static bool IsGenericTypeOf(Type vtype, Type target) {
     return
           vtype.IsGenericType
        && target.ContainsGenericParameters
        && vtype.Name == target.Name
        && vtype.Assembly == target.Assembly
        && vtype.Module == target.Module
        ;
  }