2012-04-05 42 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