简体   繁体   English

强制将静态类常量作为数据类型

[英]Enforcing static class constants as a data type

I have a static class with constants. 我有一个带有常量的静态类。 I am looking for options to create a method which takes a dictionary as an argument and enforcing the key to be one of the constants from the static class.Here is my static class with constants. 我正在寻找选项来创建一个方法,该方法将字典作为参数并强制键成为静态类的常量之一。这是我的带有常量的静态类。
在此处输入图片说明

Here is what I am trying to do 这是我想做的 在此处输入图片说明

And here is what I am trying to enforce 这就是我要强制执行的 在此处输入图片说明

From the sound of it, an Enum would be more suited to what you're trying to do. 从它的声音来看, 枚举将更适合您要尝试执行的操作。

public enum MyConstants
{
    FirstName,
    LastName,
    Title
}

public void CreateMe(Dictionary<MyConstants, string> propertyBag)
{
    ...
}

UPDATED 更新

You could combine this with attributes to associate each enum with a specific string like so: 您可以将其与属性结合使用,以将每个枚举与特定的字符串相关联,如下所示:

public enum PropertyNames
{
    [Description("first_name")]
    FirstName,
    [Description("last_name")]
    LastName,
    [Description("title")]
    Title
}

The value of each description attribute associated with each enum value could easily be grabbed via an extension method, like so: 与每个枚举值关联的每个描述属性的值可以通过扩展方法轻松地获取,如下所示:

public static class EnumExtensions
{
    public static string GetDescription(this Enum value)
    {
        FieldInfo fieldInfo = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fieldInfo.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
}

Then in your "CreateMe"-method you can get the description and value of each dictionary entry by doing something similar to this: 然后,在“ CreateMe”方法中,您可以通过执行以下操作来获取每个字典条目的描述和值:

void CreateMe(Dictionary<PropertyNames, string> propertyBag)
{
    foreach (var propertyPair in propertyBag)
    {
        string propertyName = propertyPair.Key.GetDescription();
        string propertyValue = propertyPair.Value;
    }
} 

Even though this has been already answered, there is another approach, like so: 即使已经回答了该问题,也有另一种方法,如下所示:

public class MyOwnEnum
{
    public string Value { get; private set; }

    private MyOwnEnum(string value)
    {
        Value = value;
    }

    public static readonly MyOwnEnum FirstName = new MyOwnEnum("Firstname");
    public static readonly MyOwnEnum LastName = new MyOwnEnum("LastName");
}

It behaves same way like Enum and can be used in your code with same syntax. 它的行为类似于Enum,可以在您的代码中以相同的语法使用。 I cannot give credit to whoever came up with it, but I believe I came upon it when searching for Enums with multiple values. 我不能赞扬任何提出此建议的人,但我相信在搜索具有多个值的枚举时会遇到这种情况。

With strings you can't enforce fact that keys come from limited set of vialues compile time. 对于字符串,您不能强制要求密钥来自有限的小瓶编译时间。

Use enum or custom class (possibly with implicit conversion to string) instead. 而是使用枚举或自定义类(可能会隐式转换为字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM