[英]Check if 'T' inherits or implements a class/interface
Is there a way to test if T inherits/implements a class/interface?有没有办法测试 T 是否继承/实现了一个类/接口?
private void MyGenericClass<T> ()
{
if(T ... inherits or implements some class/interface
}
There is a Method called Type.IsAssignableFrom() .有一个名为Type.IsAssignableFrom()的方法。
To check if T
inherits/implements Employee
:要检查T
继承/实现Employee
:
typeof(Employee).IsAssignableFrom(typeof(T));
If you are targeting .NET Core, the method has moved to TypeInfo:如果您的目标是 .NET Core,则该方法已移至 TypeInfo:
typeof(Employee).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo())
Note that if you want to constrain your type T
to implement some interface or inherit from some class, you should go for @snajahi's answer, which uses compile-time checks for that and genereally resembles a better approach to this problem.请注意,如果您想限制您的类型T
以实现某个接口或从某个类继承,您应该选择@snajahi 的答案,它使用编译时检查,并且通常类似于解决此问题的更好方法。
You can use constraints on the class.您可以在类上使用约束。
MyClass<T> where T : Employee
Take a look at http://msdn.microsoft.com/en-us/library/d5x73970.aspx看看http://msdn.microsoft.com/en-us/library/d5x73970.aspx
If you want to check during compilation: Error if if T
does NOT implement the desired interface/class, you can use the following constraint如果您想在编译期间检查:如果T
没有实现所需的接口/类,则错误,您可以使用以下约束
public void MyRestrictedMethod<T>() where T : MyInterface1, MyInterface2, MySuperClass
{
//Code of my method here, clean without any check for type constraints.
}
I hope that helps.我希望这有帮助。
The correct syntax is正确的语法是
typeof(Employee).IsAssignableFrom(typeof(T))
Return Value:
true
ifc
and the currentType
represent the same type, or if the currentType
is in the inheritance hierarchy ofc
, or if the currentType
is aninterface
thatc
implements, or ifc
is a generic type parameter and the currentType
represents one of the constraints ofc
, or ifc
represents a value type and the currentType
representsNullable<c>
(Nullable(Of c)
in Visual Basic).返回值:true
如果c
和当前Type
表示相同的类型,或者如果当前Type
是在继承层次结构c
,或者如果当前Type
是一个interface
即c
器具,或者如果c
是一个通用的类型参数和currentType
表示c
的约束之一,或者如果c
表示值类型并且当前Type
表示Nullable<c>
(在 Visual Basic 中为Nullable(Of c)
)。false
if none of these conditions aretrue
, or ifc
isnull
.false
如果这些条件都不是true
,或者如果c
是null
。
If Employee IsAssignableFrom T
then T
inherits from Employee
.如果Employee IsAssignableFrom T
则T
继承自Employee
。
The usage用法
typeof(T).IsAssignableFrom(typeof(Employee))
returns true
only when either仅在以下任一情况下返回true
T
and Employee
represent the same type; T
和Employee
代表同一种类型; or,要么,Employee
inherits from T
. Employee
从T
继承。 This may be intended usage in some case, but for the original question (and the more common usage), to determine when T
inherits or implements some class
/ interface
, use:在某些情况下,这可能是预期用途,但对于原始问题(以及更常见的用法),要确定T
何时继承或实现某些class
/ interface
,请使用:
typeof(Employee).IsAssignableFrom(typeof(T))
What everyone really means is:每个人真正的意思是:
typeof(BaseType).IsAssignableFrom(typeof(DerivedType)) // => true
because you can literally assign from an instance of a DerivedType
to an instance of a BaseType
:因为您可以从字面上将DerivedType
的实例分配给BaseType
的实例:
DerivedType childInstance = new DerivedType();
BaseType parentInstance = childInstance; // okay, assigning base from derived
childInstance = (DerivedType) parentInstance; // not okay, assigning derived from base
when什么时候
public class BaseType {}
public class DerivedType : BaseType {}
And some concrete examples if you're having trouble wrapping your head around it:如果您无法理解它,还有一些具体的例子:
(via LinqPad, hence the HorizontalRun
and Dump
) (通过 LinqPad,因此HorizontalRun
和Dump
)
void Main()
{
// http://stackoverflow.com/questions/10718364/check-if-t-inherits-or-implements-a-class-interface
var b1 = new BaseClass1();
var c1 = new ChildClass1();
var c2 = new ChildClass2();
var nb = new nobase();
Util.HorizontalRun(
"baseclass->baseclass,child1->baseclass,baseclass->child1,child2->baseclass,baseclass->child2,nobase->baseclass,baseclass->nobase",
b1.IsAssignableFrom(typeof(BaseClass1)),
c1.IsAssignableFrom(typeof(BaseClass1)),
b1.IsAssignableFrom(typeof(ChildClass1)),
c2.IsAssignableFrom(typeof(BaseClass1)),
b1.IsAssignableFrom(typeof(ChildClass2)),
nb.IsAssignableFrom(typeof(BaseClass1)),
b1.IsAssignableFrom(typeof(nobase))
).Dump("Results");
var results = new List<string>();
string test;
test = "c1 = b1";
try {
c1 = (ChildClass1) b1;
results.Add(test);
} catch { results.Add("FAIL: " + test); }
test = "b1 = c1";
try {
b1 = c1;
results.Add(test);
} catch { results.Add("FAIL: " + test); }
test = "c2 = b1";
try {
c2 = (ChildClass2) b1;
results.Add(test);
} catch { results.Add("FAIL: " + test); }
test = "b1 = c2";
try {
b1 = c2;
results.Add(test);
} catch { results.Add("FAIL: " + test); }
results.Dump();
}
// Define other methods and classes here
public static class exts {
public static bool IsAssignableFrom<T>(this T entity, Type baseType) {
return typeof(T).IsAssignableFrom(baseType);
}
}
class BaseClass1 {
public int id;
}
class ChildClass1 : BaseClass1 {
public string name;
}
class ChildClass2 : ChildClass1 {
public string descr;
}
class nobase {
public int id;
public string name;
public string descr;
}
baseclass->baseclass基类->基类
True真的
child1->baseclass child1->基类
False错误的
baseclass->child1基类->child1
True真的
child2->baseclass child2->基类
False错误的
baseclass->child2基类->child2
True真的
nobase->baseclass nobase->基类
False错误的
baseclass->nobase基类-> nobase
False错误的
and和
- FAIL: c1 = b1失败:c1 = b1
- b1 = c1 b1 = c1
- FAIL: c2 = b1失败:c2 = b1
- b1 = c2 b1 = c2
我相信语法是: typeof(Employee).IsAssignableFrom(typeof(T));
尽管 IsAssignableFrom 是其他人所说的最佳方式,但如果您只需要检查一个类是否从另一个类继承, typeof(T).BaseType == typeof(SomeClass)
也可以完成这项工作。
Alternate ways to tell if an object o
inherits a class or implements an interface is to use the is
and as
operators.判断对象o
继承类或实现接口的替代方法是使用is
和as
运算符。
If you want to only know if an object inherits a class or implements an interface, the is
operator will return a boolean result:如果你只想知道一个对象是继承了一个类还是实现了一个接口, is
操作符将返回一个布尔结果:
bool isCompatibleType = (o is BaseType || o is IInterface);
If you want to use the inherited class or implemented interface after your test, the as
operator will perform a safe cast, returning a reference to the inherited class or the implemented interface if compatible or null if not compatible:如果您想在测试后使用继承的类或实现的接口, as
运算符将执行安全转换,如果兼容,则返回对继承类或实现的接口的引用,如果不兼容,则返回 null:
BaseType b = o as BaseType; // Null if d does not inherit from BaseType.
IInterface i = o as IInterface; // Null if d does not implement IInterface.
If you have only the type T
, then use @nikeee's answer.如果您只有T
类型,请使用@nikeee 的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.