How to have an enum store extra info on each of it

2019-07-29 22:03发布

I have a set of items that have information on them. These items are defined by me, the programmer, and the user do not ever need to change them, they will never need to change based on configuration, and the only time they might change is in a future version of my application. I know before hand how many of these items there should be, and what their exact data is.

An enum, is a great programming construct that let's me predefine a group of keys available in my application and group them under a logical Type.

What I need now, is a construct that let's me predefine a group of keys that have extra information attached to them.

Example:

This is a standard enum:

public enum PossibleKeys
{
    Key1,
    Key2,
    Key3
}

This is the enum I would need:

public enum PossibleItems
{
    Item1
    {
        Name = "My first item",
        Description = "The first of my possible items."
    },
    Item2
    {
        Name = "My second item",
        Description = "The second of my possible items."
    },
    Item3
    {
        Name = "My third item",
        Description = "The third of my possible items."
    }
}

I know this kind of enum does not exist. What I'm asking is: How can I, in C#, hard code a set of predefined items whose data is set in code? What would be the best way to do this?

EDIT: I don't necessarily want a solution that uses enums, just a solution that allows me to have this behaviour, which is to know all possible items in my application, and the info that each of them has.

EDIT 2: It's important for me to be able to get all existing items at runtime. So being able to query all items and to iterate over them is required. Just like I could with an enum.

标签: c# oop enums
8条回答
一夜七次
2楼-- · 2019-07-29 22:57
public class PossibleItems
{
    public class Item1
    {
        public static readonly string Name = "My first item";
        public static readonly string  Description = "The first of my possible items.";
    }

    public class Item2
    {
        public static readonly string  Name = "My second item";
        public static readonly string  Description = "The second of my possible items.";
    }

    public class Item3
    {
        public static readonly string  Name = "My third item";
        public static readonly string  Description = "The third of my possible items.";
    }
}

Access using PossibleItems.Item1.Name;

查看更多
Anthone
3楼-- · 2019-07-29 23:03

When I need an associated description with the enum values, I do this by putting a DescriptionAttribute on each enum value and then use an extension method to gain access to that data. Here is a complete LINQPad example of this.

// Make sure to add a using for System.ComponentModel

void Main()
{
    Test.First.GetDescription().Dump();
    Test.Second.GetDescription().Dump();
}

public enum Test
{
    [Description("My first enum value's description.")]
    First = 1,
    [Description("My second enum value's description.")]
    Second = 2
}

public static class EnumExtensions
{
    public static string GetDescription(this Enum value)
    {
        DescriptionAttribute[] descriptionAttributes = (DescriptionAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
        return (descriptionAttributes != null && descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : null);
    }
}

This outputs

My first enum value's description.

My second enum value's description.

查看更多
登录 后发表回答