如何从VB.NET中的枚举中获取描述? [英] How to get the description from an enum in VB.NET?

查看:195
本文介绍了如何从VB.NET中的枚举中获取描述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下枚举:

 公开枚举FailureMessages 
<说明(弯曲失败)> ;
FailedCode1 = 0

<说明(剪切力失败)>
FailedCode2 = 1
结束枚举

每个枚举都有自己的描述。例如,FailedCode1有自己的描述为失败的弯曲。



下面是我的主要Sub(),我想为其分配一个变量(类型字符串)到相应的枚举。

  Sub Main()
Dim a As Integer = FailureMessages.FailedCode1
Dim b As String'I将b =对a变量a的相应描述
'这意味着:我将b将失败弯曲。在.NET中怎么做?
End Sub

任何人都可以帮助我,我怎么能在VB.NET

解决方案

您需要使用 Reflection 来检索说明。因为这些是用户添加的,因此一个或多个可能会丢失,我喜欢让它返回 Name ,如果属性缺少

 导入System.Reflection 
导入System.ComponentModel

公共共享函数GetEnumDescription(e As [Enum])As String

Dim t As Type = e.GetType()

Dim attr = CType
GetField([Enum] .GetName(t,e))
GetCustomAttribute(GetType(DescriptionAttribute)),
DescriptionAttribute)

如果attr IsNot Nothing然后
返回attr.Description
Else
返回e.ToString
如果

结束函数
/ pre>

用法:

  Dim var As FailureMessages = FailureMessages。 FailedCode1 
Dim txt As String = GetDescription(var)

您可以创建一个版本可以获取枚举的所有描述

 函数GetEnumDescriptions(Of EType)()As String()
Dim n As Integer = 0

'获取值轮询
Dim enumValues As Array = [Enum] .GetValues( GetType(EType))
'存储结果
Dim Descr(enumValues.Length - 1)As String
'获取每个值的描述或文本
对于每个值As [枚举]在枚举值
描述符(n)= GetEnumDescription(值)
n + = 1
下一个

返回描述
结束函数

用法:

  Dim descr = Utils.GetDescriptions(Of FailureMessages)()
ComboBox1.Items.AddRange(descr)

Of T 使它更容易使用。传递类型将是:

 共享函数GetEnumDescriptions(e As Type)As String()
'usage:
Dim items = Utils.GetEnumDescriptions(GetType(FailureMessages))

请注意,填充一个组合名称意味着您需要解析结果才能获得该值。相反,我发现将所有的名称和值都放在一个 List(Of NameValuePairs)中以保持它们在一起更好/更容易。



您可以将控件绑定到列表,并使用 DisplayMember 向用户显示名称,而代码使用 ValueMember 以获取实际的键入值。


I have Enum as below

Public Enum FailureMessages
  <Description("Failed by bending")>
  FailedCode1 = 0

  <Description("Failed by shear force")>
  FailedCode2 = 1
End Enum

Each enum has its own description. For example, FailedCode1 has its own description as "Failed by bending".

Below is my main Sub() where I would like to assign a variable (type string) to the corresponding enum.

 Sub Main()
  Dim a As Integer = FailureMessages.FailedCode1
  Dim b As String 'I would b = Conresponding description of variable a above
  'that means: I would b will be "Failed by bending". How could I do that in .NET ?
 End Sub 

Can anyone please help me, how could I do that in VB.NET

解决方案

You need to use Reflection to retrieve the Description. Since these are user-added and therefore one or more could be missing, I like to have it return the Nameif the Attribute is missing.

Imports System.Reflection
Imports System.ComponentModel

Public Shared Function GetEnumDescription(e As [Enum]) As String

    Dim t As Type = e.GetType()

    Dim attr = CType(t.
                    GetField([Enum].GetName(t, e)).
                    GetCustomAttribute(GetType(DescriptionAttribute)), 
                    DescriptionAttribute)

    If attr IsNot Nothing Then
        Return attr.Description
    Else
        Return e.ToString
    End If

End Function

Usage:

Dim var As FailureMessages = FailureMessages.FailedCode1
Dim txt As String = GetDescription(var)

You can create a version to get all the Descriptions for an Enum:

Friend Shared Function GetEnumDescriptions(Of EType)() As String()
    Dim n As Integer = 0

    ' get values to poll
    Dim enumValues As Array = [Enum].GetValues(GetType(EType))
    ' storage for the result
    Dim Descr(enumValues.Length - 1) As String
    ' get description or text for each value
    For Each value As [Enum] In enumValues
        Descr(n) = GetEnumDescription(value)
        n += 1
    Next

    Return Descr
End Function

Usage:

Dim descr = Utils.GetDescriptions(Of FailureMessages)()
ComboBox1.Items.AddRange(descr)

Of T makes it a little easier to use. Passing the type would be:

Shared Function GetEnumDescriptions(e As Type) As String()
' usage:
Dim items = Utils.GetEnumDescriptions(GetType(FailureMessages))

Note that populating a combo with the names means you need to parse the result to get the value. Instead, I find it better/easier to put all the names and values into a List(Of NameValuePairs) to keep them together.

You can bind the control to the list and use DisplayMember to show the name to the user, while the code uses ValueMember to get back the actual typed value.

这篇关于如何从VB.NET中的枚举中获取描述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆