enum 使用描述

论坛问题贴:路径如下

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值