enum 使用描述

来源:互联网 发布:js滑动杆打分特效 编辑:程序博客网 时间:2024/06/07 09:34

论坛问题贴:路径如下

http://social.microsoft.com/Forums/zh-CN/vbasiczhchs/thread/889fefb9-b98c-4a7a-86a2-c6359bd71cbb/?ffpr=0

 

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
原创粉丝点击