Jac*_*cek 0 .net c# string enums
我有字节值的枚举:
enum MarketingEventType : byte {MARKETING_CAMPAIGN, TELESALES, MARKETING_ACTIONS};
Run Code Online (Sandbox Code Playgroud)
我想给出所有元素名称,我将通过ToSting()方法获得.例如:
MarketingEventType.TELESALES.ToString(); // I get "bla bla bla"
MarketingEventType.MARKETING_ACTIONS.ToString(); // I get "la la la"
Run Code Online (Sandbox Code Playgroud)
从BYTE到STRING没有改变类型的enum是可能的吗?
您无法将枚举类型设置为string.有效的基本类型有byte,sbyte,short,ushort,int,uint,long,和ulong.
但是,您可以使用以下Description属性:
enum MarketingEventType
{
[Description("bla bla bla")]
TELESALES,
}
Run Code Online (Sandbox Code Playgroud)
检索枚举描述有点混乱,但你可以使用这个方法(甚至可以用它来制作扩展方法!):
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1247 次 |
| 最近记录: |