2010-09-20 34 views
0
class CBase 
{ 
object A {get;set;} 
object B {get;set;} 
} 

class CDerived : CBase 
{ 
object X {get;set} 
object Y {get;set;} 
} 

我試圖獲得第一級屬性。對於上面的例子,預期的屬性是X和Y,而不是A和B.使用下面的代碼,我得到了所有的屬性{A,B,X,Y}。有沒有沒有屬性簽名的解決方案?關於最高級別屬性的思考

foreach (var propertyInfo in typeof(CDerived).GetProperties()) 
{ 
propertyInfo.SetValue(model, row[propertyInfo.Name], null); 
} 

回答

2

嘗試使用DeclaredOnly綁定標誌的GetProperties通話。這應該限制返回到指定的繼承(class)級別的屬性。

+0

http://stackoverflow.com/questions/1544979/c-reflection-getproperties-with-bindingflags-declaredonly – 2010-09-20 22:57:48