枚举除字符串以外的返回类型? [英] Enumerate with return type other than string?

查看:82
本文介绍了枚举除字符串以外的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于枚举使用整数,因此我可以使用什么其他结构给我类似枚举的链接到名称的值:

Since enumeration uses integers, what other structure can I use to give me enum-like access to the value linked to the name:

[我知道这是错误的,寻找替代方法。

[I know this is wrong, looking for alternative]

private enum Project
    {
        Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1"),
        Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2"),
        Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1"),
        Sales = new Guid("A5690E7-1111-4AFB-B44D-1DF3AD66D435"),
        Replacement = new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"),
        Modem = new Guid("6F686C73-504B-111-9A0B-850C26FDB25F"),
        Audit = new Guid("30558C7-66D9-4189-9BD9-2B87D11190"),
        Queries = new Guid("9985242-516A-4151-B7DD-851112F562")
    }

编辑2014-07 -20

EDIT 2014-07-20

这是此问题的较新答案。将Attribute类与辅助方法一起使用,定义枚举所需的额外属性。

This is a newer answer to this question. Using the Attribute class with a helper method, define the extra attributes needed on your enum.

 public enum MultiValueEnum
    {
        [FooAttribute("alpha", 20d, true)]
        First,
        [FooAttribute("beta", 40.91d, false)]
        Second,
        [FooAttribute("gamma", 1.2d, false)]
        Third,
    }     

  public class FooAttribute : Attribute
            {
                internal FooAttribute(string name, double percentage, bool isGood)
                {
                    this.Name = name;
                    this.Percentage = (decimal)percentage;
                    this.IsGood = isGood;
                }
                public string Name { get; private set; }
                public decimal Percentage { get; private set; }
                public bool IsGood { get; private set; }
            }



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

这很容易:

        MultiValueEnum enumVar = MultiValueEnum.First;
        var enumStringValue = enumVar.GetAttribute<FooAttribute>().Name;
        var enumValueDecimal = enumVar.GetAttribute<FooAttribute>().Percentage;
        var enumBool = enumVar.GetAttribute<FooAttribute>().IsGood;


推荐答案

否则,您可以为枚举创建自定义属性,

Otherwise you could create a custom Attribute for your enum, which can hold the Guid.

这些行旁边的内容:

class EnumGuid : Attribute
{
    public Guid Guid;

    public EnumGuid(string guid)
    {
        Guid = new Guid(guid);
    }
}

然后您将像这样使用它:

And you'd then use it like so:

enum Project
{
    [EnumGuid("2ED3164-BB48-499B-86C4-A2B1114BF1")]
    Cleanup = 1,
    [EnumGuid("39D31D4-28EC-4832-827B-A11129EB2")]
    Maintenance = 2
    // and so forth, notice the integer value isn't supposed to be used, 
    // it's merely there because not assigning any value is a performance overhead.
}

最后,您可以(我总是这样做)创建一个扩展以轻松获取guid:

And finally you could (I always do this) create an extension for easily getting the guid:

static Guid GetEnumGuid(this Enum e)
{
    Type type = e.GetType();

    MemberInfo[] memInfo = type.GetMember(e.ToString());

    if (memInfo != null && memInfo.Length > 0)
    {
        object[] attrs = memInfo[0].GetCustomAttributes(typeof(EnumGuid),false);
        if (attrs != null && attrs.Length > 0)
            return ((EnumGuid)attrs[0]).Guid;
    }

    throw new ArgumentException("Enum " + e.ToString() + " has no EnumGuid defined!");
}

最后,您只需要使用枚举即可:

So in the end all you have to with your enums is:

Guid guid = Project.Cleanup.GetEnumGuid();

我使用这种方法将描述附加到枚举(通常是较长的包含空格的字符串)上,因此无法使用作为名称。

I use this approach to attach descriptions to enums, typically longer strings containing spaces, which thus cannot be used as names.

这篇关于枚举除字符串以外的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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