静态类中的常量

时间:2014-10-29 13:14:38

标签: 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 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.";
            }
        }
    }
}

现在的问题是,如果我在其中定义所有内容,这将会大大增加。 将其拆分为不同/部分类或文件夹结构的最佳方法是什么,以便保持可维护性。请记住......为了获得价值,我总是希望强制用户开始使用Constants.Labels ....

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

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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);

        }
    }



}

答案 2 :(得分:0)

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

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

这样你就可以达到每个最低级别一个类文件的目标。

答案 3 :(得分:0)

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

<强> ------------ 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";
            }
        }
    }
}

<强> ------------ Errors.cs:------------

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

如果有人应该为我的问题发布更好的解决方案,我会很乐意接受这个作为正确的答案。

相关问题