【问题标题】:How can I get the display name from my enum如何从我的枚举中获取显示名称
【发布时间】:2015-12-03 21:42:43
【问题描述】:

我进行了研究,似乎大多数人都在围绕我遇到的问题进行研究。

@Code
@Imports System.ComponentModel
Dim values = New SelectList([Enum].GetNames(GetType(myEnum)).GetAttribute<DisplayAttribute>()
End Code

最后一个括号下面有一条蓝线,当悬停时告诉我需要一个表达式。我想从我的枚举中捕获显示名称,并尝试了很多在谷歌搜索中找到的东西但没有成功。为什么会出现表达式预期错误?

已尝试合并,现在得到了结束括号
Dim type = typeof(MyEnum) ls 是预期的。

【问题讨论】:

  • VB版stackoverflow.com/a/25477800/1070452(假设“显示名”表示DescriptionAttribute)
  • 导入 System.Reflection 和 System.ComponentModel Dim value = [Enum].GetNames(GetType(MyEnum) Dim descry as New List(of String) For each v in value descry.Add(GetDescription .(Ctype(v, [Enum]))) 告诉我 GetDescription 没有声明

标签: vb.net razor enums


【解决方案1】:

您可能想看看来自 Jon Skeet 的名为 UnconstrainedMelody 的超棒 NuGet 包。

https://www.nuget.org/packages/UnconstrainedMelody/

枚举和委托的有用静态方法(或扩展方法),具有无法用常规 C# 表达的约束。

看看函数UnconstrainedMelody.Enums.GetNames()和UnconstrainedMelody.Enums.GetValues()

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多