论坛问题贴:路径如下
Imports System |
Imports System.Reflection |
Enum Coolness As Byte |
<Description("Not so cool")> _ |
NotSoCool = 5 |
Cool |
End Enum |
Class Description |
Inherits Attribute |
Public Text As String |
Public Sub New(ByVal text__1 As String) |
Text = text__1 |
End Sub |
End Class |
Module Module1 |
Private Function GetDescription(ByVal en As [Enum]) As String |
Dim type As Type = en.[GetType]() |
Dim memInfo As MemberInfo() = type.GetMember(en.ToString()) |
If memInfo IsNot Nothing AndAlso memInfo.Length > 0 Then |
Dim attrs As Object() = memInfo(0).GetCustomAttributes(GetType(Description), False) |
If attrs IsNot Nothing AndAlso attrs.Length > 0 Then |
Return DirectCast(attrs(0), Description).Text |
End If |
End If |
Return en.ToString() |
End Function |
Sub Main(ByVal args As String()) |
Dim coolType1 As Coolness = Coolness.Cool |
Dim coolType2 As Coolness = Coolness.NotSoCool |
Console.WriteLine(GetDescription(coolType1)) |
Console.WriteLine(GetDescription(coolType2)) |
Console.ReadLine() |
End Sub |
End Module |
enum 使用描述
最新推荐文章于 2021-08-10 16:10:07 发布