枚举实用程序库 [英] Enumeration Utility Library

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

问题描述

我在找一个开源库或实例与枚举类型的.Net工作。此外,人们使用枚举(TypeParse等)标准的扩展,我需要一种方法来好像回到Description属性的值对于给定的枚举值或返回一个枚举值有一个描述属性值进行操作匹配一个给定的字符串。

例如:

  //如果扩展方法
VAR赛= Race.FromDescription(AA)//返回Race.AfricanAmerican
//和
字符串raceDescription = Race.AfricanAmerican.GetDescription()//返回AA
 

解决方案

如果没有一个已经,开始一个!你也许可以找到所有你在这里的StackOverflow其他答案需要的方法 - 只要把它们卷成一个项目。这里有一些让你开始:

<一个href="http://stackoverflow.com/questions/1331487/how-to-have-userfriendly-names-for-enumerations/1331497#1331497">Getting的枚举值说明:

 公共静态字符串GetDescription(枚举值)
{
    字段信息字段= value.GetType()getfield命令(value.ToString())。
    [对象] attribs = field.GetCustomAttributes(typeof运算(DescriptionAttribute),真));
    如果(attribs.Length大于0)
    {
        返回((DescriptionAttribute)attribs [0])说明。
    }
    返回的String.Empty;
}
 

<一个href="http://stackoverflow.com/questions/1424971/parse-string-to-enum-type/1424986#1424986">Getting从字符串可为空枚举值:

 公共静态类EnumUtils
{
    公共静态可空&LT; T&GT;解析&LT; T&GT;(字符串输入),其中T:结构
    {
        //因为我们不能做一个泛型类型约束
        如果(!typeof运算(T).IsEnum)
        {
            抛出新的ArgumentException(通用型T必须是一个枚举);
        }
        如果(!string.IsNullOrEmpty(输入))
        {
            如果(Enum.GetNames(typeof运算(T))。在任何(
                  E =&GT; e.Trim()。ToUpperInvariant()== input.Trim()。ToUpperInvariant()))
            {
                返程(T)Enum.Parse(typeof运算(T),输入,真正的);
            }
        }
        返回null;
    }
}
 

I'm looking for a open source library or examples for working with Enum types in .Net. In addition to the standard extensions that people use for Enums (TypeParse, etc.), I need a way to perform operations like returning the value of the Description attribute for a given enumeration value or to return a enumeration value that has a Description attribute value that matches a given string.

For example:

//if extension method
var race = Race.FromDescription("AA") // returns Race.AfricanAmerican
//and
string raceDescription = Race.AfricanAmerican.GetDescription() //returns "AA"

解决方案

If there isn't one already, start one! You can probably find all the methods you need from other answers here on Stackoverflow - just roll them into one project. Here's a few to get you started:

Getting value of enum Description:

public static string GetDescription(this Enum value)
{
    FieldInfo field = value.GetType().GetField(value.ToString());
    object[] attribs = field.GetCustomAttributes(typeof(DescriptionAttribute), true));
    if(attribs.Length > 0)
    {
        return ((DescriptionAttribute)attribs[0]).Description;
    }
    return string.Empty;
}

Getting a nullable enum value from string:

public static class EnumUtils
{
    public static Nullable<T> Parse<T>(string input) where T : struct
    {
        //since we cant do a generic type constraint
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException("Generic Type 'T' must be an Enum");
        }
        if (!string.IsNullOrEmpty(input))
        {
            if (Enum.GetNames(typeof(T)).Any(
                  e => e.Trim().ToUpperInvariant() == input.Trim().ToUpperInvariant()))
            {
                return (T)Enum.Parse(typeof(T), input, true);
            }
        }
        return null;
    }
}

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

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