从 PropertyInfo 获取 DisplayAttribute 属性
class SomeModel
{
[Display(Name = "Quantity Required")]
public int Qty { get; set; }
[Display(Name = "Cost per Item")]
public int Cost { get; set; }
}
我正在尝试将模型映射到 { PropertyName, DisplayName }
对列表中,但我陷入了困境。
var properties
= typeof(SomeModel)
.GetProperties()
.Select(p => new
{
p.Name,
p.GetCustomAttributes(typeof(DisplayAttribute),
false).Single().ToString()
}
);
上面的内容无法编译,我不确定这是否是正确的方法,但希望您能看到其意图。有什么指点吗?谢谢
class SomeModel
{
[Display(Name = "Quantity Required")]
public int Qty { get; set; }
[Display(Name = "Cost per Item")]
public int Cost { get; set; }
}
I'm trying to map the model into a list of { PropertyName, DisplayName }
pairs, but I've got stuck.
var properties
= typeof(SomeModel)
.GetProperties()
.Select(p => new
{
p.Name,
p.GetCustomAttributes(typeof(DisplayAttribute),
false).Single().ToString()
}
);
The above doesn't compile and I'm not sure it's the right approach anyway, but hopefully you can see the intent. Any pointers? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您需要为匿名类型定义特定的属性名称。
In this case you need to define specific property names for anonymous type.