从iEnumerable< AnonymousType>获得值
我有代码可以从 propertyinfo 中获取 displayNameatTribute 并将其返回,
{Propertyname = "", DisplayName = ""}
public static IEnumerable GetDisplayNames<T>() => typeof(T).GetProperties()
.Where(p => p.IsDefined(typeof(DisplayNameAttribute), false))
.Select(p => new
{
PropertyName = p.Name,
DisplayName = p.GetCustomAttributes(typeof(DisplayNameAttribute),
false).Cast<DisplayNameAttribute>().Single().DisplayName
});
这很好,但是我想我不太了解匿名类型想知道如何返回特定项目的价值(甚至可能 - 尽管必须这样做)。
我认为它的作品类似于字典&lt; tkey,tvalue&gt;,但现在我知道不是。 简化问题:
如何通过这种结构迭代?
I've got code to get DisplayNameAttribute from PropertyInfo and return it as a Pair
{Propertyname = "", DisplayName = ""}
public static IEnumerable GetDisplayNames<T>() => typeof(T).GetProperties()
.Where(p => p.IsDefined(typeof(DisplayNameAttribute), false))
.Select(p => new
{
PropertyName = p.Name,
DisplayName = p.GetCustomAttributes(typeof(DisplayNameAttribute),
false).Cast<DisplayNameAttribute>().Single().DisplayName
});
It would be fine, but I guess I don't understand anonymous types too well and I was wondering how to return value for specific item (if it is even possible - though it has to).
I thought it works like Dictionary<Tkey, TValue>, but now I know it is not.
To simplify question:
How can I iterate through such construction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想获得具有属性和显示名称的枚举,那么...
使用元组:
If you want to get an enumeration with tuples of property and display names, then ...
... use tuples:
还是您正在寻找一个词典,例如:
Or could it be your are looking for a dictionary like: