2014-01-22 53 views
1

如何查找PropertyInfo是否實現特定類(因此也必須是類)。我知道如何檢查的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 

如何更改代碼,以便在第二個結果也是如此?

回答

6
public static bool IsOfType(PropertyInfo member) 
{ 
    return typeof(Foo).IsAssignableFrom(member.PropertyType); 
} 
相關問題