2014-11-24 89 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循环将通过所有属性。