Lookup类使用enum,struct,public const,还有别的吗?

Jan*_*n_V 5 c# lookup dictionary constants

我正在创建一个查找类,因此将在所有项目中使用常量值.问题是,有几种解决方案可以创造这样的东西.我可以创建一个包含枚举,结构或常量的单个类,或者为每个"对象"创建一个类.我想知道什么是最好的解决方案.

首先我想做这样的事情:

public static class Defines
    {
        public enum PAGELAYOUT_NAMES
        {
            STANDARD = "Standard"
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我个人不喜欢在枚举中使用字符串.另一个选择是使用一个结构,如果你看到代码,它会更难看:

public static class Defines
    {
        public struct PAGELAYOUT_NAMES
        {
            public static string STANDAARD = "Standaard";
        }
    }
Run Code Online (Sandbox Code Playgroud)

这看起来好一点,但在有很多选择时可能会让人感到困惑:

public static class Defines
{
        public const string PAGELAYOUT_NAMES_STANDARD = "Standard";
}
Run Code Online (Sandbox Code Playgroud)

在输入这篇文章时,我认为这将是最好的/干净的选择:

public static class PageLayout
{
    public const string STANDARD = "Standard";
}
Run Code Online (Sandbox Code Playgroud)

还有其他建议吗?使用几个只定义一些常量的类来填充项目在我看来像很多开销和混乱.

编辑 在原始上下文中不是很清楚,但查找值不仅限于字符串.下面的一些非常好的建议只有在您只使用字符串时才有可能,但也需要支持Int,DateTime和其他类型.从这里的答案中得到了一些不错的想法,我将尝试哪一个在我当前的项目中效果最好.

最终实现的解决方案 感谢下面的建议,我已经实现了这样的查找类:

 internal class Base<T>
    {
        internal T Value{ get; private set;}
        internal Base(T value)
        {
            Value = value;
        }
    }
    public class PageLayout
    {
        public static string Standard { get { return new Base<string>("Standard").Value; } }
    }
Run Code Online (Sandbox Code Playgroud)

这是基于下面给出的答案.原因是因为现在我也可以将它用于非字符串和整数,这对于带有描述和资源文件的枚举来说实际上是不可能的,即使这对我来说会更干净.

sof*_*eda 2

我更喜欢这种使用工厂风格静态属性的方式。但这取决于具体的场景。您可以使用字符串或枚举作为字段。

 public class PageLayout
    {
        private readonly string LayoutType;
        private PageLayout(string layoutType)
        {
          LayoutType = layoutType;
        }
        public static Standard {get {return new PageLayout("Standard");}}
    }
Run Code Online (Sandbox Code Playgroud)

然后在调用代码中使用PageLayout.Standard