Imports System.Runtime.CompilerServices
Imports System.Reflection
Public Class EnumDescription
Inherits Attribute
Private _text As String
Sub New(ByVal text As String)
_text = text
End Sub
ReadOnly Property Text
Get
Return _text
End Get
End Property
End Class
Public Module EnumExtensions
<Extension()> _
Public Function ToDescription(enumeration As [Enum]) As String
Dim type As Type = enumeration.GetType()
Dim memInfo As MemberInfo() = type.GetMember(enumeration.ToString())
If (memInfo IsNot Nothing) AndAlso (memInfo.Length > 0) Then
Dim attrs As Object() = memInfo(0).GetCustomAttributes(GetType(EnumDescription), False)
If (attrs IsNot Nothing) AndAlso (attrs.Length > 0) Then
Return (TryCast(attrs(0), EnumDescription)).Text
End If
End If
Return enumeration.ToString()
End Function
End Module
获取枚举类型Enum的注释内容。
最新推荐文章于 2024-07-26 19:48:56 发布