【问题标题】:Constants in static classes静态类中的常量
【发布时间】:2014-12-25 05:11:38
【问题描述】:

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

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 开始......

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

【问题讨论】:

  • 这是什么语言?
  • 使用资源文件
  • 你似乎在使用像命名空间这样的嵌套类。
  • @RubenHerman 您要求最佳方法来保持此可维护性。 最好的方式是资源文件。如果你有奇怪的限制,不要要求最好的方法(这通常也是非常主观的)。
  • @RubenHerman,那为什么不使用资源文件作为密钥呢? IE。对资源文件中的键和值使用相同的值。在资源文件中维护这种类型的信息似乎比在类文件中更容易。

标签: c# static constants


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    这样您就可以实现每个最低级别一个类文件的目标。

    【讨论】:

    • 确实,但我希望开发人员在访问值时始终键入全名。如果他们只是导入命名空间,他们可以立即开始使用 TopNavigation.Save
    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:
      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);
      
              }
          }
      
      
      
      }
      

      【讨论】:

      • 常量初始化器必须是编译时常量。所以不起作用
      • 你说得对,实际上我尝试翻译一个我在 Java 中使用过的解决方案。
      • 我用一个可行的解决方案编辑了代码。但现在看起来不那么干净了。我的情况是代码是自动生成的,所以没那么重要。
      猜你喜欢
      • 2010-09-09
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      相关资源
      最近更新 更多