如何消除字段中的枚举,而不指定命名空间? [英] How do I disambiguate enums from fields without specifying the namespace?
问题描述
enum SameName {Value}
class Tester
{
void Method1(){
SameName SameName;
SameName test = SameName.Value;
}
void Method2(){
string SameName;
SameName test = SameName.Value;
}
}
在第一种方法中,编译器正确地将SameName .Value指枚举。
在第二种方法中,编译器感到困惑,认为SameName.Value指的是字符串类的Value成员。因为没有这样的成员存在,它的错误。
我可以做些什么来帮助编译器在此上下文中更了解我的SameName枚举?有没有办法使用语句?
- 我无法重命名我的实际代码中的变量或枚举。 li>
- 我正在创建一个包含许多枚举值的大型字典,我宁愿在枚举的每个实例前添加命名空间前缀。
更新:是的,我知道我不应该使用大写的局部变量。是的,我意识到这些是局部变量,而不是字段。是的,我意识到,如果真正的代码看起来像这样,调试是非常糟糕的。我写了上面这个简短的例子,显示了我在真实代码中遇到的问题。我很抱歉,我没有明确表达我的意图。在真实代码中,类位于枚举名称空间中的一个命名空间中,局部变量是基类中的一个属性。我试图删除所有这些无关的代码,使得这个问题更容易发现,并列出了我的要求,为这个问题提供了一个空白。
OK,根据您的更新,我想我们可以帮助您。这种情况现在更有意义。我想有代码:
//在一个你不允许编辑的地方
namespace Outer
{
public enum SameName {Value,}
}
和<在
名称空间BaseSpace
{
public class TesterBase
{
public string SameName {get;组; }
}
}
然后你在第三个代码中遇到问题您可以编辑的文件。我建议您使用 使用
alias指令指向类型。那么你就不必一遍又一遍地重复完整的命名空间。它将如下所示:
使用BaseSpace;
使用snEnum = Outer.SameName; //这有助于您(使用别名)
命名空间Outer.Inner
{
public class Tester:TesterBase
{
void Method2(){
snEnum test = snEnum.Value;
}
}
}
请注意,使用别名指令指向命名空间或类型(就像这种情况)。他们给你一个简单的简写,你使用了很多繁琐的名字。
enum SameName { Value }
class Tester
{
void Method1() {
SameName SameName;
SameName test = SameName.Value;
}
void Method2() {
string SameName;
SameName test = SameName.Value;
}
}
In the first method, the compiler correctly divines that SameName.Value refers to the enum.
In the second method, the compiler gets confused and thinks that SameName.Value is referring to a Value member of the string class. Since no such member exists, it errors.
What can I do to help make the compiler more aware of my SameName enum in this context? Is there some way to do this with using statements?
- I can't rename the variable or the enum in my real code.
- I'm creating a large dictionary with many enum values, and I'd rather not add the namespace prefix in front of each instance of the enum.
Update: Yes, I know I shouldn't use capitalized local variables. Yes, I realize these are local variables, not fields. Yes, I realize that this would be terrible to debug if the real code looked like this. I wrote the above as a short, contrived example that shows the problem I'm having in real code. I apologize that I did not make my intent clear. In the real code, the class is in a namespace contained within the enum's namespace, and the local variable is a property in a base class. I've tried to remove all that extraneous code to make the concern easier to spot, and listed my requirements to give a little scope for the problem.
OK, based on your update, I think we can help you. The situation makes more sense now. I suppose there's code:
// in a place you're not allowed to edit
namespace Outer
{
public enum SameName { Value, }
}
and
// in a place you're not allowed to edit
namespace BaseSpace
{
public class TesterBase
{
public string SameName { get; set; }
}
}
and then you have the problem in a third code file which you can edit. I suggest you solve it with a using
alias directive pointing to the type. Then you won't have to repeat the full namespace over and over again. It will look like this:
using BaseSpace;
using snEnum = Outer.SameName; // this helps you (a using alias)
namespace Outer.Inner
{
public class Tester : TesterBase
{
void Method2() {
snEnum test = snEnum.Value;
}
}
}
Note that using alias directives can "point" either to a namespace or to a type (like in this case). They give you a shorthand for a cumbersome name that you use a lot.
这篇关于如何消除字段中的枚举,而不指定命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!