C# 静态类中的常量

C# 静态类中的常量,c#,static,constants,C#,Static,Constants,通过使用静态类(和常量字符串),我希望能够获得如下常量值: var value = Constants.Labels.Controls.TopNavigation.Save; 我为此问题创建了此结构: public static class Constants { public static class Labels { public static class Controls { public static class

通过使用静态类(和常量字符串),我希望能够获得如下常量值:

var value = Constants.Labels.Controls.TopNavigation.Save;
我为此问题创建了此结构:

public static class Constants
{
    public static class Labels
    {
        public static class Controls
        {
            public static class TopNavigation
            {
                public const string Save = "Save";
                public const string Refresh = "Refresh";
            }
        }

        public static class General
        {
            public static class Errors
            {
                public const string UnexpectedError = "An unexpected error occured.";
            }
        }
    }
}
现在的问题是,如果我定义其中的所有内容,这将大大增加。 将其拆分为不同/部分类或文件夹结构以保持可维护性的最佳方法是什么。牢记为了得到这个值,我总是要求用户从常量开始


如果可能,我还希望每个最低级别有一个类文件…

您可以使用资源文件或XML文件将它们存储为密钥对。

如果类层次结构除了组织常量之外没有提供任何值,为什么不使用名称空间呢

using System;

namespace Test
{

    public class TopNavigationConst {
        private const string SAVE = "Save";
        private const string REFRESH = "Refresh";

        public String Save {get{return SAVE; }}
        public String Refresh {get{return REFRESH;}}
    }

    public class ErrorsConst
    {
        public const string UNESPECTEDERROR = "An unexpected error occured.";
        public String UnexpectedError {get{return UNESPECTEDERROR; }}
    }

    public class ControlsConst 
    {
        private TopNavigationConst topNavigation = new TopNavigationConst();
        public TopNavigationConst TopNavigation {get{return topNavigation;}}
    }

     public class GeneralConst
    {
        public ErrorsConst errors = new ErrorsConst();
        public ErrorsConst Errors {get{return errors;}}
    }

    public class LabelsConst
    {
        public static ControlsConst controls = new ControlsConst();
        public static GeneralConst general = new GeneralConst();

        public ControlsConst Controls {get{return controls;}}
        public GeneralConst General {get{return general;}}
    }

    public class Constants
    {
        public static LabelsConst labels = new LabelsConst();
        public static LabelsConst Labels {get{return labels;}}
    }

    public class Test
    {
        public static void Main()
        {
            var value = Constants.Labels.Controls.TopNavigation.Save;
            System.Console.WriteLine(value);

        }
    }



}
namespace Constants.Labels.Controls
{
    public static class TopNavigation
    {
        public const string Save = "Save";
        public const string Refresh = "Refresh";
    }
}
这样,您就可以实现每个最低级别一个类文件的目标

这个问题的最佳解决方案(目前)是使用分部类。缺点:每个文件都有一些重复的结构。优点:当程序扩展时,此文件不会变大。它在更多的文件中分开,我需要使用全名来获得正确的值。因此,这是我首选的解决方案:

-----------TopNavigation.cs:-----------

public partial class Constants
{
    public partial class Labels
    {
        public partial class Controls
        {
            public partial class TopNavigation
            {
                public const string Save = "LABELS_CONTROLS_TOPNAVIGATION_SAVE";
                public const string New = "LABELS_CONTROLS_TOPNAVIGATION_NEW";
            }
        }
    }
}
public partial class Constants
{
    public partial class Labels
    {
        public partial class General
        {
            public partial class Errors
            {
                public const string Unexpected = "LABELS_GENERAL_ERRORS_UNEXPECTED";
                public const string EmptyArgument = "LABELS_GENERAL_ERRORS_EMPTYARGUMENT";
            }
        }
    }
}
------错误。cs:------

public partial class Constants
{
    public partial class Labels
    {
        public partial class Controls
        {
            public partial class TopNavigation
            {
                public const string Save = "LABELS_CONTROLS_TOPNAVIGATION_SAVE";
                public const string New = "LABELS_CONTROLS_TOPNAVIGATION_NEW";
            }
        }
    }
}
public partial class Constants
{
    public partial class Labels
    {
        public partial class General
        {
            public partial class Errors
            {
                public const string Unexpected = "LABELS_GENERAL_ERRORS_UNEXPECTED";
                public const string EmptyArgument = "LABELS_GENERAL_ERRORS_EMPTYARGUMENT";
            }
        }
    }
}

如果有人能为我的问题提供更好的解决方案,我很乐意接受这一正确答案。

这是什么语言?使用资源文件您似乎在使用命名空间之类的嵌套类。@RubenHerman您询问了保持此可维护性的最佳方法。最好的方法是资源文件。如果你有奇怪的限制,不要问最好的方法(这通常也是非常主观的)。@RubenHerman,那么为什么不使用资源文件作为密钥呢?即,对密钥和资源文件中的值使用相同的值。在资源文件中维护此类信息似乎比在类文件中维护此类信息更容易。事实上,我希望开发人员在访问值时始终键入全名。如果只是导入名称空间,则可以立即使用TopNavigation启动。SaveConstant初始值设定项必须是编译时常量。你是对的,事实上我试着翻译一个我在Java中使用的解决方案,我用一个可行的解决方案编辑了代码。但现在它看起来不那么干净了。在我的案例中,代码是自动生成的,所以它不那么重要。