2014-11-24 88 views
0

我使用MVC和c#..我怎樣才能得到屬性的價值?我如何獲得自定義屬性道具的價值?

謝謝

foreach (var prop in this.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(CalcProgressAttribute)))) 
{ 

//i need prop 's value here 

} 
+0

嘗試'對象值= prop.GetValue(this,null);' – 2014-11-24 22:48:09

+0

它運行。謝謝 – Yargicx 2014-11-24 22:49:28

回答

1

試試這個,使GetProperty工作,你希望:

foreach (var prop in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(prop => Attribute.IsDefined(prop, typeof(CalcProgressAttribute)))) 
{ 

object value = prop.GetValue(this, null); 

} 

我不知道你閹循環foreach作品,因爲我無法證明以下部分您的代碼: .Where(prop => Attribute.IsDefined(prop, typeof(CalcProgressAttribute))))。但沒有Where循環將通過所有屬性。