返回类型不是字符串的枚举?

发布于 2024-07-26 15:33:37 字数 2227 浏览 8 评论 0原文

由于枚举使用整数,我可以使用什么其他结构来对链接到名称的值进行类似枚举的访问:

[我知道这是错误的,正在寻找替代方案]

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

这是对此的更新答案问题。 使用带有辅助方法的 Attribute 类,定义枚举所需的额外属性。

 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;

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")
    }

EDIT 2014-07-20

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

Which makes it this easy:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

焚却相思 2024-08-02 15:33:37

否则,您可以为枚举创建一个自定义属性,它可以保存 Guid。

沿着这些线的东西:

class EnumGuid : Attribute
{
    public Guid Guid;

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

然后你会像这样使用它:

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:

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!");
}

所以最后你对枚举所要做的就是:

Guid guid = Project.Cleanup.GetEnumGuid();

我使用这个将描述附加到枚举的方法,通常是包含空格的较长字符串,因此不能用作名称。

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

Something alongside these lines:

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

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.

凉栀 2024-08-02 15:33:37

我已经看到 SubSonic 使用这个方法(结构)来存储列和表名称。

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

编辑:- 感谢您对代码缺陷的评论。 首先,如果 Guid 字符串不是无效的,它将编译。 至于不创建实例来访问变量,是的,它们需要是公共静态

I've seen this method (struct) used by SubSonic to store Column and Table names.

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

EDIT:- Thanks for commenting on deficiencies in code. In first place it will compile if the Guid strings are not invalid. As for not create instances to access variables yes they need to be public static

紧拥背影 2024-08-02 15:33:37

我可能会在这方面走字典路线。 基本上有一个查找表:

public class GuidMapper
{
    private Dictionary<GuidTypes, Guid> mGuidMap = new Dictionary<GuidTypes, Guid>();
    public enum GuidTypes: int
    {
        Cleanup,
        Maintenance,
        Upgrade,
        Sales,
        Replacement,
        Modem,
        Audit,
        Queries
    }

    public GuidMapper()
    {
        mGuidMap.Add(GuidTypes.Cleanup, new Guid("2ED31640-BB48-499B-86C4-A2B1114BF100"));
        mGuidMap.Add(GuidTypes.Maintenance, new Guid("39D31D40-28EC-4832-827B-A11129EB2000"));
        mGuidMap.Add(GuidTypes.Upgrade, new Guid("892F8650-E38D-46D7-809A-49510111C100"));
        mGuidMap.Add(GuidTypes.Sales, new Guid("A5690E70-1111-4AFB-B44D-1DF3AD66D435"));
        mGuidMap.Add(GuidTypes.Replacement, new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"));
        mGuidMap.Add(GuidTypes.Modem, new Guid("6F686C73-504B-1110-9A0B-850C26FDB25F"));
        mGuidMap.Add(GuidTypes.Audit, new Guid("30558C70-66D9-4189-9BD9-2B87D1119000"));
        mGuidMap.Add(GuidTypes.Queries, new Guid("99852420-516A-4151-B7DD-851112F56200"));
    }

    public Guid GetGuid(GuidTypes guidType)
    {
        if (mGuidMap.ContainsKey(guidType))
        {
            return mGuidMap[guidType];
        }
        return Guid.Empty;
    }
}

I would probably go the dictionary route on this one. Have a lookup table basically:

public class GuidMapper
{
    private Dictionary<GuidTypes, Guid> mGuidMap = new Dictionary<GuidTypes, Guid>();
    public enum GuidTypes: int
    {
        Cleanup,
        Maintenance,
        Upgrade,
        Sales,
        Replacement,
        Modem,
        Audit,
        Queries
    }

    public GuidMapper()
    {
        mGuidMap.Add(GuidTypes.Cleanup, new Guid("2ED31640-BB48-499B-86C4-A2B1114BF100"));
        mGuidMap.Add(GuidTypes.Maintenance, new Guid("39D31D40-28EC-4832-827B-A11129EB2000"));
        mGuidMap.Add(GuidTypes.Upgrade, new Guid("892F8650-E38D-46D7-809A-49510111C100"));
        mGuidMap.Add(GuidTypes.Sales, new Guid("A5690E70-1111-4AFB-B44D-1DF3AD66D435"));
        mGuidMap.Add(GuidTypes.Replacement, new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"));
        mGuidMap.Add(GuidTypes.Modem, new Guid("6F686C73-504B-1110-9A0B-850C26FDB25F"));
        mGuidMap.Add(GuidTypes.Audit, new Guid("30558C70-66D9-4189-9BD9-2B87D1119000"));
        mGuidMap.Add(GuidTypes.Queries, new Guid("99852420-516A-4151-B7DD-851112F56200"));
    }

    public Guid GetGuid(GuidTypes guidType)
    {
        if (mGuidMap.ContainsKey(guidType))
        {
            return mGuidMap[guidType];
        }
        return Guid.Empty;
    }
}
赠佳期 2024-08-02 15:33:37

如果您需要正确的类似于枚举的语义和类型安全,那么您可以使用这样的模式。

(如果您需要转换运算符、GetUnderlyingTypeToString 等额外功能,您可以进一步充实它。如果您想为多个 enum< 重复使用该模式/code> 类具有不同的底层类型,那么您可以将任何通用代码移动到通用的抽象基类中。)

Project x = Project.Cleanup;
Project y = Project.Cleanup;
Project z = Project.Maintenance;

Console.WriteLine(x == y);     // True
Console.WriteLine(x == z);     // False
Console.WriteLine(x.Value);    // 47801daa-7437-4bfe-a240-9f7c583018a4

// this line will cause a compiler error
Console.WriteLine(x == new Guid("47801daa-7437-4bfe-a240-9f7c583018a4"));

// ...

public class Project
{
    private Project(Guid v) { Value = v; }
    public Guid Value { get; private set; }

    public static readonly Project Cleanup =
        new Project(new Guid("47801daa-7437-4bfe-a240-9f7c583018a4"));

    public static readonly Project Maintenence =
        new Project(new Guid("2548a7f3-7bf4-4533-a6c1-dcbcfcdc26a5"));

    public static readonly Project Upgrade =
        new Project(new Guid("ed3c3e73-8e6a-4c09-84ae-7f0876d194aa"));
}

If you need proper enum-like semantics and type-safety then you can use a pattern like this.

(You could flesh it out further if you require extras like conversion operators, GetUnderlyingType, ToString etc. If you wanted to re-use the pattern for multiple enum-like classes with different underlying types then you could move any common code into a generic, abstract base class.)

Project x = Project.Cleanup;
Project y = Project.Cleanup;
Project z = Project.Maintenance;

Console.WriteLine(x == y);     // True
Console.WriteLine(x == z);     // False
Console.WriteLine(x.Value);    // 47801daa-7437-4bfe-a240-9f7c583018a4

// this line will cause a compiler error
Console.WriteLine(x == new Guid("47801daa-7437-4bfe-a240-9f7c583018a4"));

// ...

public class Project
{
    private Project(Guid v) { Value = v; }
    public Guid Value { get; private set; }

    public static readonly Project Cleanup =
        new Project(new Guid("47801daa-7437-4bfe-a240-9f7c583018a4"));

    public static readonly Project Maintenence =
        new Project(new Guid("2548a7f3-7bf4-4533-a6c1-dcbcfcdc26a5"));

    public static readonly Project Upgrade =
        new Project(new Guid("ed3c3e73-8e6a-4c09-84ae-7f0876d194aa"));
}
蓝海 2024-08-02 15:33:37

当遇到这种问题时,我使用带有 const 的结构作为公共成员:

public struct FileExtensions
{
    public const string ProcessingExtension = ".lck";
    public const string ProcessedExtension = ".xml";
    public const string FailedExtension = ".failed";
    public const string CsvExtension = ".csv";
}

When confronted with this kind of problem I used structs with consts as public members:

public struct FileExtensions
{
    public const string ProcessingExtension = ".lck";
    public const string ProcessedExtension = ".xml";
    public const string FailedExtension = ".failed";
    public const string CsvExtension = ".csv";
}
つ可否回来 2024-08-02 15:33:37

您可以创建一个仅包含常量值的静态类。
例如:

internal static class Project
{
    public static readonly Guid Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
    public static readonly Guid Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
    public static readonly Guid Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1");
}

这样,类就简单地充当容器,并且不能从中创建对象。

在 VB 中,这将是一个模块:

Friend Module Project
    Public Shared ReadOnly Cleanup As Guid = New Guid("2ED3164-BB48-499B-86C4-A2B1114BF1")
    Public Shared ReadOnly Maintenance As Guid = New Guid("39D31D4-28EC-4832-827B-A11129EB2")
    Public Shared ReadOnly Upgrade As Guid = New Guid("892F865-E38D-46D7-809A-49510111C1")
End Module

You could create a static class that just contains constant values.
For example:

internal static class Project
{
    public static readonly Guid Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
    public static readonly Guid Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
    public static readonly Guid Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1");
}

This way the class acts simply as a container and object cannot be created from it.

In VB this would be a Module:

Friend Module Project
    Public Shared ReadOnly Cleanup As Guid = New Guid("2ED3164-BB48-499B-86C4-A2B1114BF1")
    Public Shared ReadOnly Maintenance As Guid = New Guid("39D31D4-28EC-4832-827B-A11129EB2")
    Public Shared ReadOnly Upgrade As Guid = New Guid("892F865-E38D-46D7-809A-49510111C1")
End Module
素手挽清风 2024-08-02 15:33:37

枚举类型仅支持整数类型(char 除外)作为其值。 但是,您可以使用字典之类的东西来查找名称和值。

Dictionary<Guid> lookup = new Dictionary<Guid>();
lookup["Cleanup"] = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
lookup["Maintenance"] = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
lookup["Upgrade"] = new Guid("892F865-E38D-46D7-809A-49510111C1");
// etc...

另一种选择是在静态类中包含一系列只读值。

public static class Guids
{
  public static readonly Guid Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
  public static readonly Guid Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
  public static readonly Guid Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1");
}

The enum type can only support the integral types (except char) as its value. You could however use something like a Dictionary to do lookups of a a name to a value.

Dictionary<Guid> lookup = new Dictionary<Guid>();
lookup["Cleanup"] = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
lookup["Maintenance"] = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
lookup["Upgrade"] = new Guid("892F865-E38D-46D7-809A-49510111C1");
// etc...

Another alternative is to have a series of readonly values in a static class.

public static class Guids
{
  public static readonly Guid Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
  public static readonly Guid Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
  public static readonly Guid Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文