2012-04-05 46 views
2
public class ReflectionBase 
    { 
     public String ParentProperty1 { get; set; } 
     public String ParentProperty2 { get; set; }   
    } 

    public class Reflection : ReflectionBase 
    { 
     public String ChildProperty1 { get; set; } 

     public Reflection() 
     { 
      var property = this.GetType().GetProperties(); 
     }  
    } 

結果:
ParentProperty1
ParentProperty2
ChildProperty1
我需要:
ChildProperty1

當我打電話的GetProperties()它給了我一切當前的類屬性和基類,但我只需要當前的類屬性。

任何幫助,請...如何讓子類屬性不是超和子一起

回答

9

使用BindingFlags.DeclaredOnly忽略繼承成員:

var properties = this.GetType().GetProperties(
    BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); 
+1

它工作得很好,謝謝。 – 2012-04-05 19:48:47