C# 是否有可能测试一个泛型的基础?
好的,这听起来可能有点奇怪,但我需要测试传递给我的C# 是否有可能测试一个泛型的基础?,c#,generics,typechecking,C#,Generics,Typechecking,好的,这听起来可能有点奇怪,但我需要测试传递给我的对象是否属于ModelItem类型,我不关心t实际上是什么。换句话说,如果它是ModelItem、ModelItem或ModelItem,那么我需要返回true 注意:如果我是ModelItem的所有者,我会考虑只定义一个类型为imodeliem的接口,并将其分配为ModelItem定义的一部分,但我没有访问源代码的权限。当然,有可能: public bool IsIt(object thing) { var type = thing.G
对象是否属于ModelItem
类型,我不关心t
实际上是什么。换句话说,如果它是ModelItem
、ModelItem
或ModelItem
,那么我需要返回true
注意:如果我是ModelItem
的所有者,我会考虑只定义一个类型为imodeliem
的接口,并将其分配为ModelItem
定义的一部分,但我没有访问源代码的权限。当然,有可能:
public bool IsIt(object thing)
{
var type = thing.GetType();
if (type.IsGenericType)
{
return type.GetGenericTypeDefinition() == typeof(MyThing<>);
}
return false;
}
当然,有可能:
public bool IsIt(object thing)
{
var type = thing.GetType();
if (type.IsGenericType)
{
return type.GetGenericTypeDefinition() == typeof(MyThing<>);
}
return false;
}
True
True
True
False