[英]Reflection in C# Superclass and Subclass
Friends, 朋友们,
Given these three classes: 给定这三个类:
public class MyBaseClass {
}
public class MyDerivedClass1: MyBaseClass {
}
public class MyDerivedClass2: MyBaseClass {
}
At runtime, I will have an object of type MyBaseClass
(which will either be MyDerivedClass2
or MyDerivedClass1
). 在运行时,我将拥有一个MyBaseClass
类型的对象(它将是MyDerivedClass2
或MyDerivedClass1
)。 How can I determine, at runtime if MyBaseClass
is MyDerivedClass1
? 在运行时如何确定MyBaseClass
是否为MyDerivedClass1
?
Using the is
operator. 使用is
运算符。
is (C# Reference) - Checks if an object is compatible with a given type. is(C#参考) -检查对象是否与给定类型兼容。 For example, the following code can determine if an object is an instance of the MyObject type, or a type that derives from MyObject: 例如,以下代码可以确定对象是MyObject类型的实例还是从MyObject派生的类型:
The is operator returns true if an instance is in the inheritance tree. 如果实例在继承树中,则is运算符返回true。
if (myInstance is MyDerivedClass1)
{
// my instance is of type MyDerivedClass1
}
This will work with your sample code. 这将与您的示例代码一起使用。 BUT if you want to check if its exactly the same type you need to use typeof()
and GetType()
. 但是,如果要检查其类型是否完全相同,则需要使用typeof()
和GetType()
。
if (myInstance.GetType() == typeof (MyDerivedClass1))
{
// my instance is of type MyDerivedClass1
}
1. If you want to be sure whether the instance is exactly of the given type you should use GetType() method and compare it with desired type: 1.如果要确定实例是否完全属于给定类型,则应使用GetType()方法并将其与所需类型进行比较:
bool IsExactlyOfMyDerivedClass2(object instance)
{
if (instance == null)
throw new ArgnumentNullException();
return (instance.GetType() == typeof(MyDerivedClass2))
}
or generic version 或通用版本
bool IsExactlyOf<T>(object instance)
{
if (instance == null)
throw new ArgnumentNullException();
return (object.GetType() == typeof(T))
}
2. If you does not care whether the instance is exactly of the given type (or the type is abstract class or interface) you, as it has been pointed by @dknaack use the IS C# operator: 2.如果您不关心实例是否完全属于给定类型(或类型是抽象类或接口),那么@dknaack已指出该实例使用IS C#运算符:
bool IsOfMyDerivedClass2_OrMoreDerived(object instance)
{
if (instance == null)
throw new ArgnumentNullException();
return instance is MyDerivedClass;
}
3. Also, you can also use the IsAssignable method of Type class: 3.此外,您还可以使用Type类的IsAssignable方法:
bool IsOfMyDerivedClass2_OrMoreDerived(object instance)
{
if (instance == null)
throw new ArgnumentNullException();
return typeof(MyDerivedClass2).IsAssignableFrom(instance.GetType());
}
or generic version: 或通用版本:
bool IsOf_OrMoreDerived<T>(object instance)
{
if (instance == null)
throw new ArgnumentNullException();
return typeof(T).IsAssignableFrom(instance.GetType());
}
If you don't know the types at compile time (ie you only have access to Type objects), you can use Type.IsAssignableFrom (or TypeInfo.IsAssignableFrom depending on which Platform you are). 如果您在编译时不知道类型(即您只能访问Type对象),则可以使用Type.IsAssignableFrom (或TypeInfo.IsAssignableFrom,具体取决于您使用的平台)。
var baseClassType = typeof(BaseClass);
var derivedClassType = typeof(DerivedClass);
var isBaseClass = baseClassType.IsAssignableFrom(derivedClassType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.