fea*_*net 17 .net c# reflection propertyinfo data-annotations
class SomeModel
{
[Display(Name = "Quantity Required")]
public int Qty { get; set; }
[Display(Name = "Cost per Item")]
public int Cost { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将模型映射到对列表中{ PropertyName, DisplayName },但我已经卡住了.
var properties
= typeof(SomeModel)
.GetProperties()
.Select(p => new
{
p.Name,
p.GetCustomAttributes(typeof(DisplayAttribute),
false).Single().ToString()
}
);
Run Code Online (Sandbox Code Playgroud)
以上不编译,我不确定这是正确的方法,但希望你能看到意图.有什么指针吗?谢谢
Kir*_*huk 27
您需要为匿名类型定义不同的属性名称.
var properties = typeof(SomeModel).GetProperties()
.Where(p => p.IsDefined(typeof(DisplayAttribute), false))
.Select(p => new
{
PropertyName = p.Name, p.GetCustomAttributes(typeof(DisplayAttribute),
false).Cast<DisplayAttribute>().Single().Name
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14046 次 |
| 最近记录: |