如何让枚举存储每个条目的额外信息

Did*_* A. 4 c# oop enums

我有一组包含相关信息的项目。这些项目是由我(程序员)定义的,用户不需要更改它们,它们永远不需要根据配置进行更改,并且它们唯一可能更改的时间是在我的应用程序的未来版本中。我事先知道这些项目应该有多少,以及它们的确切数据是什么。

枚举是一种很棒的编程结构,它让我可以预定义应用程序中可用的一组键,并将它们分组到逻辑类型下。

我现在需要的是一个构造,让我可以预定义一组附加了额外信息的键。

例子:

这是一个标准枚举:

public enum PossibleKeys
{
    Key1,
    Key2,
    Key3
}
Run Code Online (Sandbox Code Playgroud)

这是我需要的枚举:

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."
    }
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:我不一定想要一个使用枚举的解决方案,只是一个允许我具有这种行为的解决方案,即了解我的应用程序中所有可能的项目以及它们每个项目所拥有的信息。

编辑2:对我来说能够在运行时获取所有现有项目很重要。因此,需要能够查询所有项目并迭代它们。就像我可以使用枚举一样。

McA*_*den 6

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

像这样的东西:

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);
}
Run Code Online (Sandbox Code Playgroud)

然后检索:

KeyMetadataLookup[PossibleKeys.Key1].Description
Run Code Online (Sandbox Code Playgroud)

请注意,只有在某些属性无法处理的情况下我才会使用它。如果都是原始类型,您也可以简单地创建自己的自定义属性。您不仅仅局限于内置的。

您自己的自定义属性最终将类似于:

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

    public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后在使用中:

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
}
Run Code Online (Sandbox Code Playgroud)


小智 5

尝试这个

public enum PossibleKeys{
    [Description("key 1 desc")]
    Key1,
    [Description("key 2 desc")]
    Key2,
    [Description("key 3 desc")]
    Key3
}
Run Code Online (Sandbox Code Playgroud)