如何让一个枚举存储每个进入的额外的信息 [英] How to have an enum store extra info on each of it's entry

查看:136
本文介绍了如何让一个枚举存储每个进入的额外的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组有关于这些信息的项目。这些项目由我定义,程序员和用户不需要更改它们,他们将永远不需要根据配置进行更改,并且唯一可能更改的时间是我的应用程序的将来版本。我知道手中有多少这些项目应该是,以及他们的确切数据。

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.

示例:

这是一个标准枚举:

public enum PossibleKeys
{
    Key1,
    Key2,
    Key3
}

这是我需要的枚举:

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

我知道这种枚举不存在。我要问的是:在C#中,如何在一个代码中设置数据的预定义项目的硬编码?什么是最好的方法?

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?

编辑:我不一定需要一个使用枚举的解决方案,只是一个解决方案,让我有这种行为,这是要知道我的应用程序中的所有可能的项目,以及每个人的信息。

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.

编辑2:对我来说,能够获得所有现有项目很重要在运行时。因此,可以查询所有项目并对其进行迭代。

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.

推荐答案

如果纯粹是为了描述,你可以使用内置的 DescriptionAttribute 如其他一些答案所述。但是,如果您需要属性无法提供的功能,则可以使用某种元数据对象创建查找。

If it's purely for description you can use the built-in DescriptionAttribute as stated in some of the other answers. If you need functionality that an attribute can't supply, however, you can create a lookup with some sort of metadata object.

这样的一个例子:

public enum PossibleKeys
{
    Key1,
    Key2,
    Key3
}

public class KeyMetadata
{
    public PossibleKeys Id { get; set; }
    public string Description { get; set; }
    public SomeOtherClass SomethingAttributesCantHandle { get; set; }
}

private static readonly IReadOnlyDictionary<PossibleKeys, KeyMetadata> KeyMetadataLookup;

static EnumContainerClass()
{
    Dictionary<PossibleKeys, KeyMetadata> metadata = new Dictionary<PossibleKeys, KeyMetadata>();
    metadata.Add(PossibleKeys.Key1, new KeyMetadata { Id = PossibleKeys.Key1, Description = "First Item" });
    metadata.Add(PossibleKeys.Key2, new KeyMetadata { Id = PossibleKeys.Key2, Description = "Second Item" });
    metadata.Add(PossibleKeys.Key3, new KeyMetadata { Id = PossibleKeys.Key3, Description = "Third Item" });
    KeyMetadataLookup = new ReadOnlyDictionary<PossibleKeys, KeyMetadata>(metadata);
}

然后检索:

KeyMetadataLookup[PossibleKeys.Key1].Description

注意如果有某些属性无法处理,我只会使用它。如果这是所有原始类型,您也可以简单地制作自己的自定义属性。您不仅仅是内置的。

Note that I'd only use this if there was something attributes couldn't handle. If it's all primitive types you can also simply make your own custom attribute. You're not limited to simply the built-in ones.

您自己的自定义属性最终将如下所示:

Your own custom attribute would end up like:

[System.AttributeUsage(System.AttributeTargets.Enum)]
public class CustomDataAttribute : System.Attribute
{
    public string Name { get; set; }

    public string Description { get; set; }
}

然后使用:

public enum PossibleItems
{
    [CustomData(Name = "My first item", Description = "The first of my possible items.")]
    Item1,

    [CustomData(Name = "My second item", Description = "The second of my possible items.")]
    Item2,

    [CustomData(Name = "My third item", Description = "The third  of my possible items.")]
    Item3
}

这篇关于如何让一个枚举存储每个进入的额外的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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