我已经定义了以下内容enum
:
public enum DeviceType
{
[Description("Set Top Box")]
Stb = 1,
Panel = 2,
Monitor = 3,
[Description("Wireless Keyboard")]
WirelessKeyboard = 4
}
Run Code Online (Sandbox Code Playgroud)
我正在利用该Description
属性允许我提取更多用户可读的枚举版本以在UI中显示.我使用以下代码获得描述:
var fieldInfo = DeviceType.Stb.GetType().GetField(DeviceType.Stb.ToString());
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
var description = (attributes.Length > 0 ? attributes[0].Description : DeviceType.Stb.ToString());
Run Code Online (Sandbox Code Playgroud)
上面的代码将给我:description = "Set Top Box"
.如果没有Description
设置属性,它将为我提供枚举的字符串值.
我现在想为每个枚举添加第二个/自定义属性(为了示例,称为"值").例如:
public enum DeviceType
{
[Description("Set Top Box")]
[Value("19.95")]
Stb = 1,
[Value("99")]
Panel = 2,
[Value("199.99")]
Monitor = 3,
[Description("Wireless Keyboard")]
[Value("20")]
WirelessKeyboard = 4
}
Run Code Online (Sandbox Code Playgroud)
我需要提取新Value
属性,就像我目前使用Description
属性一样.
是否可以将现有Description
属性扩展为以某种方式包含新Value
属性,还是最好分别创建新属性?
Ayd*_*din 17
创建一个单独称为DeviceInformation的新属性...
[AttributeUsage(AttributeTargets.All)]
public class DeviceInformationAttribute : DescriptionAttribute
{
public DeviceInformationAttribute(string description, string value)
{
this.Description = description;
this.Value = value;
}
public string Description { get; set; }
public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用扩展方法来检索任何属性的值
static void Main(string[] args)
{
var info = DeviceType.Stb.GetAttribute<DeviceInformationAttribute>();
Console.WriteLine("Description: {0}\nValue:{1}",info.Description, info.Value);
}
public static class Extensions
{
public static TAttribute GetAttribute<TAttribute>(this Enum enumValue)
where TAttribute : Attribute
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<TAttribute>();
}
}
public enum DeviceType
{
[DeviceInformation("foobar", "100")]
Stb = 1,
}
Run Code Online (Sandbox Code Playgroud)
回应评论
@Aydin Adn我喜欢使用扩展方法,非常好!您是否有针对DeviceType.Panel案例的解决方案,该解决方案没有描述,但需要Value属性?(见Patrick的回答评论)
[AttributeUsage(AttributeTargets.All)]
public class DeviceInformationAttribute : Attribute
{
public DeviceInformationAttribute(string description)
{
this.Description = description;
}
public DeviceInformationAttribute(decimal value)
{
this.Description = string.Empty;
this.Value = value;
}
public DeviceInformationAttribute(string description, decimal value)
{
this.Description = description;
this.Value = value;
}
public string Description { get; set; }
public decimal Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Pat*_*man 13
是的,这很容易做到.只需派生现有的DescriptionAttribute
类:
[AttributeUsageAttribute(AttributeTargets.All)]
public class DescriptionWithValueAttribute : DescriptionAttribute
{
public DescriptionWithValueAttribute(string description, string value) : base(description)
{
this.Value = value;
}
public string Value { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它:
public enum DeviceType
{
[DescriptionWithValue("Set Top Box", "19.95")]
Stb = 1,
}
Run Code Online (Sandbox Code Playgroud)
检索属性的代码将保持几乎相同,只需替换类型名称即可.