取得Enum程式碼中的描述

先import 下面這2個namespace

Imports System.ComponentModel
Imports System.Reflection

<FlagsAttribute()> _
Public Enum Permissions
 
<Description("未設定")> _
None = 0
 
<Description("建立")> _
Create = 1
 
<Description("讀取")> _
Read = 2
 
<Description("更新")> _
Update = 4
 
<Description("刪除")> _
Delete = 8
 
<Description("所有功能")> _
All = Create Or Read Or Update Or Delete
 
End Enum
 
'要取得描述時使用下面的程式碼
 
Dim fi As FieldInfo = _
Permissions.Create.GetType().GetField(Permissions.Create.ToString())
 
Dim attributes As DescriptionAttribute() = _
CType(fi.GetCustomAttributes(GetType(DescriptionAttribute), False),  _
DescriptionAttribute())
 
'顯示取得的描述
Response.Write(attributes(0).Description)

 

新增評論

Loading