0
我使用MVC和c#..我怎样才能得到属性的价值?我如何获得自定义属性道具的价值?
谢谢
foreach (var prop in this.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(CalcProgressAttribute))))
{
//i need prop 's value here
}
我使用MVC和c#..我怎样才能得到属性的价值?我如何获得自定义属性道具的价值?
谢谢
foreach (var prop in this.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(CalcProgressAttribute))))
{
//i need prop 's value here
}
试试这个,使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
循环将通过所有属性。
尝试'对象值= prop.GetValue(this,null);' – 2014-11-24 22:48:09
它运行。谢谢 – Yargicx 2014-11-24 22:49:28