如何查找propertyinfo是否暗示特定类(因此也必须是类)。我知道如何检查PropertyInfo是否具有特定类型,但这不起作用用于检查它是否派生类型:
public class Foo
{
public Foo foo { get; set; }
public Bar bar { get; set; }
public void CheckStuff()
{
foreach (var property in this.GetType().GetProperties())
Debug.WriteLine(Bar.IsOfType(property));
}
}
public class Bar : Foo
{
public static bool IsOfType(PropertyInfo member)
{
return member.PropertyType == typeof(Foo);
}
}
结果:
True
False
如何更改代码,因此第二个结果也是正确的?
public static bool IsOfType(PropertyInfo member)
{
return typeof(Foo).IsAssignableFrom(member.PropertyType);
}