【问题标题】:Intellisense with many constants (nesting?)具有许多常量的智能感知(嵌套?)
【发布时间】:2015-09-09 09:49:13
【问题描述】:

场景:有一个字符串常量列表(现在超过 100 个,将来会更多),定义如下:

public const string ChildA = "Child A";
public const string ChildASomeParameter = "Some parameter";
public const string ChildASomeOtherParameter = "Some other parameter";
...
public const string ChildB = "Child B"; 
public const string ChildBSomeParameter = "Some different parameter"; 
public const string ChildBSomeOtherParameter = "Some parameter";  // values are not unique
...

问题:将它与智能感知一起使用并不好:当需要ChildA 时,您可能会得到智能感知提供ChildASomeParameter99999。滚动该名称或完全输入名称显然效率不高。

我想将参数移动到嵌套类型中,像这样

public const string ChildA = "Child A";
public class ChildA
{
    public const string SomeParameter1 = "Some parameter 1";
    public const string SomeParameter2 = "Some parameter 2";
}

但是还有一个编译时间问题:

“MyConstants”类型已经包含“ChildA”的定义

我需要有关如何处理该问题的帮助(想法)。

我的想法:

  • ChildA 放入另一个嵌套类型?这将增加开销:ChildA 的使用频率将比它的任何参数更频繁using static 在这里非常出色,必须输入类似 Childs.ChildA 的内容并不快。
  • 重命名?将ChildA 重命名为丑陋的_ChildA?或者将嵌套类型重命名为丑陋的东西?那么什么名字不丑呢?

也许有人知道更好的方法?

【问题讨论】:

  • 例如公共类 ChildA { public const string Name = "ChildA"; }
  • @bushed,这听起来是迄今为止最好的选择。我仍然希望有更好的选择(我不知道或没有想到的)。
  • 如果常量不能被分离并与特定的类相关联,并且实际上只是通用常量(即错误代码列表或类似的东西),我很可能会接受打击,并且接受将涉及打字。它们显然属于一起,所以应该在一起,并且至少名称是明确而简洁的,即使打字不愉快。要么这样,要么为常量的重复部分提供适当的缩写。
  • 显然 nameof() 被认为是编译时常量。例如。可以放在属性里。如果您可以只使用属性名称而不是字符串,它可能会解决问题 public class A : Attribute { public A(string name) { } public const string Name = nameof(Name); } [A(nameof(A.Name))] 公共类 X { }
  • 你有性能方面的考虑吗?

标签: c# .net constants intellisense


【解决方案1】:

在当前课程中使用Current 怎么样?

static void Main(string[] args)
{
    System.Console.WriteLine(Constants.ChildA.Current);
    System.Console.WriteLine(Constants.ChildB.SomeOtherParameter);
    System.Console.WriteLine(Constants.ChildA.SomeParameter);

}

public static class Constants
{
    public static class ChildA
    {
        public const string Current = "Child A";
        public const string SomeParameter = "Some parameter";
        public const string SomeOtherParameter = "Some other parameter";
    }

    public static class ChildB
    {
        public const string Current = "Child B";
        public const string SomeParameter = "Some different parameter";
        public const string SomeOtherParameter = "Some parameter";
    }
}

【讨论】:

  • using static Constants; 让它变得更好(与普通的常量列表相比)。避免使用Current 真的很酷,但也许没有办法。
【解决方案2】:

使用Namespaces

Using Namespaces (C# Programming Guide)

像这样:

namespace N1     // N1
{
  class C1      // N1.C1
  {
      class C2   // N1.C1.C2
      {
      }
  }
  namespace N2  // N1.N2
  {
      class C2   // N1.N2.C2
      {
      }
  }
}

【讨论】:

  • 你能根据我的代码调整你的例子吗?我看不出命名空间与类型嵌套有什么不同,或者使用命名空间有什么优势? ChildAChildASomeParameter 是在哪里定义的?
  • 我的意思是命名空间可以很容易地使用相同的类型名称,但@dotctor 有更好的主意。
【解决方案3】:

选项 1:

public class ChildA
{
    public const string Name = "Child A"
    public const string SomeParameter1 = "Some parameter 1";
    public const string SomeParameter2 = "Some parameter 2";
}

选项 2(有约束):

public class ChildA
{
    public const string SomeParameter1 = nameof(SomeParameter1);
    public const string SomeParameter2 = nameof(SomeParameter2);
}

用法:nameof(ChildA), nameof(ChildA.SomeParameter1)

详细说明约束:

  • 不能使用任意字符串
  • nameof 仅返回顶级名称。例如。 nameof(ChildA.ChildAA.Param1) == "Param1"

【讨论】:

  • 我们可以使用“枚举”代替选项2。
  • 问题是nameof(ChildA) != "Child A"(注意空格),实际上有"Some property (A)"之类的值(包含不能用作成员名称的字符)。
  • @Chandru 你的意思不是常量字符串——这会限制进一步的嵌套——你不能在枚举中嵌套类
  • @Chandru,带字符串的枚举?不,我们不能。
  • 我的意思是这样而不是 option2 public enum ChildA { ChildA, SomeParameter1, SomeParameter2 } 无论如何我们不能使用这个选项,因为我们需要单词之间的空格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 2011-02-25
  • 1970-01-01
相关资源
最近更新 更多