多个枚举描述 [英] Multiple enum descriptions

查看:136
本文介绍了多个枚举描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了以下枚举

 公开枚举的devicetype 
{
[描述(机顶盒)]
机顶盒= 1,
屏= 2,
监视器= 3,
[描述( 无线键盘)]
WirelessKeyboard = 4
}

我利用说明属性,让我拉出枚举用户更可读的版本在UI中显示。我得到使用下面的代码说明:

  VAR字段信息= DeviceType.Stb.GetType()getfield命令(DeviceType.Stb。的ToString()); 

VAR属性=(DescriptionAttribute [])fieldInfo.GetCustomAttributes(typeof运算(DescriptionAttribute),FALSE);

Var描述=(attributes.Length大于0的属性[0]。说明:DeviceType.Stb.ToString());



将上面的代码给我:描述=机顶盒。如果没有说明属性设置,它会给我的枚举的字符串值。



我现在想添加第二个/自定义属性到每个枚举(呼吁例子着想值)。例如:

 公开枚举的devicetype 
{
[说明(机顶盒)]
[值(19.95)]
机顶盒= 1,
[值(99)]
面板= 2,
[值(199.99)]
监视器= 3,
[描述(无线键盘)]
[数值(20)]
WirelessKeyboard = 4
}

我将需要拉出新的属性大同小异这样,我现在做的说明属性。



是否有可能扩大现有的说明属性以某种方式包括新的属性,或者是它最好单独创建新的属性?


< DIV CLASS =h2_lin>解决方案

创建一个新的属性称为单独... DeviceInformation

  [AttributeUsage(AttributeTargets.All)
公共类DeviceInformationAttribute:DescriptionAttribute
{
公共DeviceInformationAttribute(字符串描述,字符串值)
{
this.Description =说明;
THIS.VALUE =价值;
}

公共字符串描述{搞定;组; }
公共字符串值{获得;组; }
}

您还可以使用扩展方法来检索任何属性的值

 静态无效的主要(字串[] args)
{
VAR信息= DeviceType.Stb.GetAttribute< DeviceInformationAttribute>();
Console.WriteLine(说明:{0} \\\
Value:{1},info.Description,info.Value);

}

公共静态类扩展
{
公共静态TAttribute的getAttribute< TAttribute>(枚举enumValue)
式TAttribute:属性
{
返回enumValue.GetType()
.GetMember(enumValue.ToString())
。首先()
.GetCustomAttribute< TAttribute>();
}
}

公共枚举的devicetype
{
[DeviceInformation(FOOBAR,100)]
机顶盒= 1,
}






修改



在回应注释




@Aydin ADN我爱使用扩展方法, 非常好!你有不具有一个描述DeviceType.Panel的情况下的解决方案,但是需要的值属性? (见帕特里克的答案评论)




  [AttributeUsage(AttributeTargets.All)] 
酒店的公共类DeviceInformationAttribute:属性
{
公共DeviceInformationAttribute(字符串描述)
{
this.Description =说明;
}

公共DeviceInformationAttribute(十进制值)
{
this.Description =的String.Empty;
THIS.VALUE =价值;
}

公共DeviceInformationAttribute(字符串描述,十进制值)
{
this.Description =说明;
THIS.VALUE =价值;
}


公共字符串描述{搞定;组; }
公共十进制值{搞定;组; }
}


I have defined the following enum:

public enum DeviceType
{
    [Description("Set Top Box")]
    Stb = 1,
    Panel = 2,
    Monitor = 3,
    [Description("Wireless Keyboard")]
    WirelessKeyboard = 4
}

I'm utilising the Description attribute to allow me to pull out a more user-readable version of the enum to display in the UI. I get the description using the following code:

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());

The above code will give me: description = "Set Top Box". If there is no Description attribute set, it will give me the string value of the enum.

I now want to add a second/custom attribute to each of the enums (called 'Value' for examples sake). eg:

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
}

I will need to pull out the new Value attribute much the same way I currently do with the Description attribute.

Is it possible to extend the existing Description attribute to somehow include the new Value attribute, or is it best to create the new attribute separately?

解决方案

Create a new attribute seperately called 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; }
}

You can also use the extension method to retrieve the value of any attribute

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,
}


Edit

In response to the comment

@Aydin Adn I do love the use of the extension method, very nice! Do you have a solution for the case of DeviceType.Panel which does not have a description, but needs the Value attribute? (see comments on Patrick's answer)

 [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; }
}

这篇关于多个枚举描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆