从iEnumerable< AnonymousType>获得值

发布于 2025-01-30 07:46:13 字数 786 浏览 3 评论 0原文

我有代码可以从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2025-02-06 07:46:13

如果您想获得具有属性和显示名称的枚举,那么...

使用元组

public static IEnumerable<(string propertyName, string displayName)> GetDisplayNames<T>()
    => typeof(T).GetProperties()
        .Where(p => p.IsDefined(typeof(DisplayNameAttribute), false))
        .Select(p =>
            (
                propertyName: p.Name,
                displayName: p.GetCustomAttributes(typeof(DisplayNameAttribute), false)
                                 .Cast<DisplayNameAttribute>().Single().DisplayName
            )
        );

If you want to get an enumeration with tuples of property and display names, then ...

... use tuples:

public static IEnumerable<(string propertyName, string displayName)> GetDisplayNames<T>()
    => typeof(T).GetProperties()
        .Where(p => p.IsDefined(typeof(DisplayNameAttribute), false))
        .Select(p =>
            (
                propertyName: p.Name,
                displayName: p.GetCustomAttributes(typeof(DisplayNameAttribute), false)
                                 .Cast<DisplayNameAttribute>().Single().DisplayName
            )
        );
以往的大感动 2025-02-06 07:46:13

还是您正在寻找一个词典,例如:

public static Dictionary<string, string> GetDisplayNames<T>() =>
    typeof(T).GetProperties()
        .Where(p => p.IsDefined(typeof(DisplayNameAttribute), false))
        .ToDictionary(
            p => p.Name, // Key
            p.GetCustomAttributes(typeof(DisplayNameAttribute), false) 
             .Cast<DisplayNameAttribute>()         
             [0] // Linq not really required
             .DisplayName); // Value

Or could it be your are looking for a dictionary like:

public static Dictionary<string, string> GetDisplayNames<T>() =>
    typeof(T).GetProperties()
        .Where(p => p.IsDefined(typeof(DisplayNameAttribute), false))
        .ToDictionary(
            p => p.Name, // Key
            p.GetCustomAttributes(typeof(DisplayNameAttribute), false) 
             .Cast<DisplayNameAttribute>()         
             [0] // Linq not really required
             .DisplayName); // Value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文