从值中获取 VB.net 枚举描述

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18888519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:10:19  来源:igfitidea点击:

Get VB.net Enum Description from Value

vb.netenums

提问by doovers

How can I get Enumdescription from its value?

如何Enum从其值中获取描述?

I can get the description from the name using:

我可以使用以下名称从名称中获取描述:

Public Shared Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
    Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
    Dim attr() As DescriptionAttribute = _ 
                  DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), _
                  False), DescriptionAttribute())

    If attr.Length > 0 Then
        Return attr(0).Description
    Else
        Return EnumConstant.ToString()
    End If
End Function 

But I cant figure out how to pass a variable name to this function. I've tried things like

但是我不知道如何将变量名传递给这个函数。我试过这样的事情

GetEnumDescription([Enum].GetName(GetType(myEnum), 2)))

but nothing I've tried is correct.

但我尝试过的一切都不正确。

采纳答案by Heinzi

If you have a variable of your enum type, it's simply

如果你有一个枚举类型的变量,它只是

GetEnumDescription(myEnum)

Minimal working example:

最小工作示例:

Enum TestEnum
    <Description("Description of Value1")>
    Value1
End Enum

Public Sub Main()
    Dim myEnum As TestEnum = TestEnum.Value1
    Console.WriteLine(GetEnumDescription(myEnum)) ' prints "Description of Value1"
    Console.ReadLine()
End Sub


If you have an Integervariable, you need to cast it to your enum type first (CTypeworks as well):

如果你有一个Integer变量,你需要先将它转换为你的枚举类型(CType也可以):

GetEnumDescription(DirectCast(myEnumValue, TestEnum))

Working example:

工作示例:

Enum TestEnum
    <Description("Description of Value1")>
    Value1 = 1
End Enum

Public Sub Main()
    Console.WriteLine(GetEnumDescription(DirectCast(1, TestEnum)))
    Console.ReadLine()
End Sub

The source for your confusion seems to be a misunderstanding: Your method does not take the "name" of an enum as a parameter, it takes an Enumas a parameter. That's something different, and it's also the reason why your attempts to use GetNamefailed.

您混淆的根源似乎是一种误解:您的方法不将枚举的“名称”作为参数,而是将 anEnum作为参数。那是不同的东西,这也是您尝试使用GetName失败的原因。

回答by mikro

Here's another solution to get an Enum's description as an extension.

这是将枚举的描述作为扩展名的另一种解决方案。

Imports System.ComponentModel
Imports System.Runtime.CompilerServices

<Extension()> Public Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
    Dim attr() As DescriptionAttribute = DirectCast(EnumConstant.GetType().GetField(EnumConstant.ToString()).GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
    Return If(attr.Length > 0, attr(0).Description, EnumConstant.ToString)
End Function

Example use from the previous posts:

以前帖子中的示例使用:

Enum Example
    <Description("Value1 description.")> Value1 = 1
    <Description("Value2 description.")> Value2 = 2
End Enum

Sub Main()
    Console.WriteLine(DirectCast(2, Example).GetEnumDescription())
    Console.ReadLine()
End Sub