Visual Studio在不需要时建议完全限定的命名空间

Tre*_*ott 15 c# intellisense visual-studio

使用Visual Studio 2010(可能还有2008),我注意到Intellisense将为枚举建议完全限定名称空间的行为.

例如,我可以编写如下代码:

element.HorizontalAlignment = HorizontalAlignment.Right;
element.VerticalAlignment = VerticalAlignment.Bottom;
Run Code Online (Sandbox Code Playgroud)

但是当我尝试写它时,它建议我这样写:

element.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
element.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
Run Code Online (Sandbox Code Playgroud)

这个不必要的额外代码可以真正加起来并使其可读性降低,我必须基本上与Intellisense对抗以避免它.

我有理由这样吗?我可以把它关掉吗?我假设原因是枚举的名称与属性的名称相同.但那真的不是一个好理由.

编辑:

这是另一个演示为什么不需要完全限定命名的示例.

using SomeOtherNamespace;

namespace SomeNamespace
{
    public class Class1
    {
        public Class2 Class2 { get; set; }

        public Class1()
        {
            // These all compile fine and none require fully qualified naming.  The usage is context specific.
            // Intellisense lists static and instance members and you choose what you wanted from the list.

            Class2 = Class2.Default;
            Class2.Name = "Name";
            Class2.Name = Class2.Default.Name;
            Class2 = Class2;
        }
    }
}

namespace SomeOtherNamespace
{
    public class Class2
    {
        public static Class2 Default { get; set; }

        // public static Class2 Class2;  (This throws an error as it would create ambiguity and require fully qualified names.)

        // public static string Name { get; set; }  (This also throws an error because it would create ambiguity and require fully qualified names.

        public string Name { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tig*_*ran 6

你在WPF环境中工作(我看到元素),你有某种方式System.Windows.Formsdll 的引用.

我的推论是基于HorizontalAlignment两个名称空间中都可以找到的事实:

System.Windows.Forms.Horizo​​ntalAlignment中

System.Windows.FrameworkElement.Horizo​​ntalAlignment中

有两个引用指向同一个类型,VS要求指定什么名称空间究竟你的意思.


NSG*_*aga 5

这确实似乎是same name for property & the type.
这是smallest reproducible example模仿事物(可能更小,但这显示更多)...

namespace Company.Project.SubProject.Area.Test.AndSomeMore
{
    public class TestClass
    {
        public TestEnum MyEnum { get; set; }
        public TestEnum TestEnum { get; set; }
        public SndTestEnum NewEnum { get; set; }
    }
    public enum TestEnum
    {
        None,
        One,
        Two
    }
    public enum SndTestEnum
    {
        None,
        One,
        Two
    }
}
namespace MyCallerNS
{
    public class MyTestClass : TestClass
    {
        public MyTestClass()
        {
            this.TestEnum = Company.Project.SubProject.Area.Test.AndSomeMore.TestEnum.One;
            this.MyEnum = Company.Project.SubProject.Area.Test.AndSomeMore.TestEnum.Two;
            this.NewEnum = SndTestEnum.None;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

两者MyEnumTestEnum属性(目标TestEnum枚举)提供"完全限定"的名称(其他名称与其属性类型不同,但类型与其他属性的名称匹配,因此两者都被"污染") - 而SndTestEnum具有不同的命名(对于类型,属性)和在任何一种情况下工作正常

...有趣的是,即使你删除namespace MyCallerNS并将所有内容放在"长名称空间"之下 - 它仍然会AndSomeMore.在前面添加.

我认为没有解决方案(没有Re#和第三方工具),
这似乎是智能感知as smart as the compiler,不像@Rick建议的那样.

或者更确切地说 - 编译器花费时间解决问题(手头有所有信息),而intellisense没有那个"深度"和对事物的洞察力(我猜,真正简化 - 我们需要@Eric这个:)和做出快速/最简单的选择.

编辑:实际上,根据我之前的想法,
它更多的是关于每个执行的"工作" - 而智能感知(作为完成'服务')必须向您提供列表中的所有选项(属性名称和类型) (我不认为它们都存在,但是猜测.并且有一个选择来覆盖两者可能会很难处理)
所以区分它会增加完全限定的名称.
它"失败"(有点)的地方是"粘贴"最终的"短版本" - 我确实应该这么想.