【发布时间】:2015-04-09 15:57:21
【问题描述】:
考虑以下情况
public class SomethingWithAReallyReallyAnnoyinglyLongName{
public struct Names
{
public const string SomeConstant = "Hello";
public const string SomeOtherConstant = "World";
}
}
在SomethingWithAReallyReallyAnnoyinglyLongName 上下文之外时,有没有一种方法可以引用SomethingWithAReallyReallyAnnoyinglyLongName.Names.SomeConstant 而不必引用SomethingWithAReallyReallyAnnoyinglyLongName?
// Won't work "Struct Name is not valid at this point."
var names = SomethingWithAReallyReallyAnnoyinglyLongName.Names;
SomeFunction(names.SomeConstant, names.SomeOtherConstant);
// Won't work "Cannot access static constant..."
var names = new SomethingWithAReallyReallyAnnoyinglyLongName.Names();
SomeFunction(names.SomeConstant, names.SomeOtherConstant);
长类名是自动生成的,所以我无法更改它,但我可能会更改 Names 结构的任何内容(使其成为一个类,将 const 更改为非 const 等)。
有什么想法吗?
【问题讨论】:
-
否 -
const字段实际上是static并且必须由类名限定。生成的是整个类还是只是名称? -
@DStanley 生成了整个类。但我可以调整 CodeDom(这是我正在做的添加名称)
-
您可以使用别名,例如 using Shortcut = SomethingWithAReallyReallyAnnoyinglyLongName.Names;然后只是 Shortcut.SomeConstant
-
上次我读到,C# 6 会有类似的东西,但我不知道它是否已经完成,或者它是否适用于非静态类。
-
Maybee this 可以提供帮助。
标签: c#