获取列表<串GT;我用枚举泛型方法属性 [英] Get a List<string> of my enum attributes with a generic method

查看:111
本文介绍了获取列表<串GT;我用枚举泛型方法属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始时,我们有这个基本的枚举。

At start, we have this basic enum.

public enum E_Levels {

    [ValueOfEnum("Low level")]
    LOW,

    [ValueOfEnum("Normal level")]
    NORMAL,

    [ValueOfEnum("High level")]
    HIGH
}

和我想获得一个列表与LT;串> 枚举任何。类似 Extensions.GetValuesOfEnum< E_Levels>()它可以返回一个列表<串> 与低级别在这正常水平和高级别

And I would like to get a List<string> whatever the enum. Something like Extensions.GetValuesOfEnum<E_Levels>() which could return a List<string> with "Low level", "Normal level" and "High level" in it.

StackOF帮我弄的有一个 value属性:

StackOF helped me to get one value attribute :

public static class Extensions {

    public static string ToValueOfEnum(this Enum value) {

        FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
        ValueOfEnum[] attribs = fieldInfo.GetCustomAttributes(typeof(ValueOfEnum), false) as ValueOfEnum[];
        return attribs.Length > 0 ? attribs[0].value : null;
    }
}

和我可以调用这个方法不管枚举: E_Levels.LOW.ToValueOfEnum()

And I can call this method whatever the enum : E_Levels.LOW.ToValueOfEnum().

此外,StackOF帮助我获得名单,LT ;串> 具体枚举。
我在一个控制器发出此方法:

Furthermore, StackOF helped me to get a List<string> for a specific enum. I made this method in a controller :

private List<string> GetLevels() {

List<string> levelsToReturn = new List<string>();
var levels = Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>();
foreach(E_Levels l in levels) 
    levelsToReturn.Add(l.ToValueOfEnum());

return levelsToReturn;
}



不过,这种方式要求我重写每个枚举的方法相同。

于是,我就加这个通用方法我班的扩展:

But this way requires me to rewrite the same method for each enum.
So I tried to add this generic method my class Extensions :

public static class Extensions {

    public static string ToValueOfEnum(this Enum value) {...}

    public static List<string> GetValuesOf<T>() {

        List<string> levelsToReturn = new List<string>();
        var levels = Enum.GetValues(typeof(T)).Cast<T>();
        foreach(T l in levels) 
        levelsToReturn.Add(l.ToValueOfEnum());

        return levelsToReturn;
    }
}



但在我的foreach, .ToValueOfEnum()是一个未知的方法。搜索结果
所以我麻烦,我希望我能够一次又一次地为每个同样的方法找到一种方法,不能重写枚举...

But in my foreach, .ToValueOfEnum() is an unknown method.

So I am in trouble, I hoped I could find a way to not rewrite again and again the same method for each enum...

推荐答案

让我们尝试保持这个更加通用的。

Let's try to keep this more general purpose.

我有可能会抢掉属性枚举值的扩展方法。这将使你快速访问属性。

I have an extension method that could grab attributes off of enum values. This would give you quick access to the attributes.

public static class EnumExtensions
{
    public static TAttribute GetAttribute<TAttribute>(this Enum value)
        where TAttribute : Attribute
    {
        var type = value.GetType();
        var name = Enum.GetName(type, value);
        return type.GetField(name)
            .GetCustomAttributes(false)
            .OfType<TAttribute>()
            .SingleOrDefault();
    }
}



利用这一点,你可以创建一些查询得到什么你想

Using this, you could create some queries to get what you want.

var valuesOfLevels =
    Enum.GetValues(typeof(E_Levels)).Cast<E_Levels>()
        .Select(level => level.GetAttribute<ValueOfEnumAttribute>().Value);



所以你的 GetValuesOf()方法(该方法是不是这样的特殊方法,恕我直言,一个伟大的名字)可以这样写的:

So your GetValuesOf() method (which is not a great name for such a specialty method IMHO) can be written like this:

public static List<string> GetValuesOf<TEnum>()
    where TEnum : struct // can't constrain to enums so closest thing
{
    return Enum.GetValues(typeof(TEnum)).Cast<Enum>()
               .Select(val => val.GetAttribute<ValueOfEnumAttribute>().Value)
               .ToList();
}

现在你可以叫像这样的方法:

Now you may call the method like so:

var levelValues = GetValueOf<E_Levels>();
// levelValues = { "Low level", "Normal level", "High level" }

这篇关于获取列表&LT;串GT;我用枚举泛型方法属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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