2012-04-22 60 views
15

使用Visual Studio 2010(可能還有2008)我注意到Intellisense會爲枚舉提供完全限定名稱空間的行爲。Visual Studio在不需要時提供完全限定名稱空間

例如,我可以這樣寫代碼:

element.HorizontalAlignment = HorizontalAlignment.Right; 
element.VerticalAlignment = VerticalAlignment.Bottom; 

但是,當我試圖寫它,它表明我寫這樣的:

element.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; 
element.VerticalAlignment = System.Windows.VerticalAlignment.Bottom; 

這種不必要的額外的代碼才能真正加起來,使其不易讀,我必須基本與智能感知戰鬥,以避免它。

我有這個理由嗎?我可以關掉它嗎?我假設原因是枚舉的名稱與該屬性的名稱相同。但這真的不是一個好理由。

編輯:

下面是說明爲什麼完全合格的命名是沒有必要的另一個例子。

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; } 
    } 
} 
+0

Intrresting;在我不記得的時候,VS從來沒有提過完全合格的名字。 – 2012-04-22 21:01:26

+0

我以前在Windows窗體應用程序中遇到過相同的問題。這是建議我編寫System.Windows.Forms.DialogResult.OK,而不是僅僅是DialogResult.OK。同樣,我很確定這是事實,在該範圍內有一個名爲「DialogResult」的本地屬性(一個表單)。 – 2012-04-22 21:07:30

+0

沒錯。我認爲這是在這種情況下需要的。 – 2012-04-22 22:28:25

回答

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

兩個MyEnumTestEnum性(靶向TestEnum枚舉)報價「完全合格的名字(其它具有比其他名稱它的屬性類型,但類型匹配其他屬性的名稱,所以都是'污點') - 而SndTestEnum有不同的命名(對於類型,屬性),並在任何情況下工作正常。

......有趣的是,即使你刪除了namespace MyCallerNS並將所有的「長命名空間」置於其下,它仍然會在前面添加AndSomeMore.

沒有解決辦法,因爲我看到它(短的RE#和第三方工具),
這似乎不是as smart as the compiler情況下,作爲@Rick建議智能感知。或者說 - 編譯器花時間來解決問題(所有的信息都在手中),而intellisense沒有那種'深度'和對事物的洞察力(我猜測,真的簡化了 - 我們需要@Eric這:),並使快/最簡單的選擇。

編輯:其實,我以前的想法,
它更多的「工作」,每個執行 - 和智能(如完成「服務」)已與所有的選擇,爲您呈現(包括財產名稱和類型)(我沒有看到它們都存在,但只是一個猜測,並且有一個選擇來覆蓋這兩個可能會很痛苦)
所以區分它會添加完全限定的名稱。
而「失敗」(有點)的地方是最後'粘貼''短版' - 這應該是我的想法。

6

您在WPF環境中工作(我看到的元素),你有莫名其妙引用System.Windows.Forms DLL。

我的演繹是基於事實HorizontalAlignment可以在兩個命名空間中找到:

System.Windows.Forms.HorizontalAlignment

System.Windows.FrameworkElement.HorizontalAlignment

有兩個引用指向同一類型

VS要求指定什麼名字空間確切地說是你的意思。

+0

我沒有對System.Windows.Forms的引用。 – 2012-04-22 20:56:41

+0

@Moozhe:任何「Office」dll,而不是? – Tigran 2012-04-22 20:57:32

+0

它是System,WindowsBase,PresentationFramework和PresentationCore的空項目。 – 2012-04-22 20:58:08

2

我發現如果我輸入element.HorizontalAlignment =那麼VS2010會自動建議System.Windows.HorizontalAlignment如果你按下tab鍵將會被選中。如果不是按下Tab鍵,而是鍵入'Ho'來縮小列表的範圍,然後按Tab鍵,您將獲得Horizo​​ntalAlignment。

如果你能夠使用Resharper,然後鍵入之後「=」您將被呈現的最明顯的選擇:

HorizontalAlignment.Center 
HorizontalAlignment.Left 
HorizontalAlignment.Stretch 
HorizontalAlignment.Right 
+0

這可能是正確的答案。我實際上研究了這個問題,並發現其他人說Resharper沒有解決這個問題,但這可能是一個較舊的版本或不好的信息。今晚我會試試這個。 – 2012-04-25 15:59:50

+0

我正在使用R#6.1和7.我不記得之前版本中的行爲。 – Phil 2012-04-25 16:01:16

+0

我試過R#6.1和7.有趣的是,它打破了默認的智能感知預選。在Resharper選項 - >環境 - >智能感知 - > Autopopup - > C#下,它提到了Preselecting作爲一個選項,但它沒有預先選擇,實際上即使您選擇「Display and do preselect」,行爲似乎也是一樣的。 – 2012-04-26 03:05:02

1

這是編譯器比智能感知更聰明的情況下。

如果您要訪問與其類型名稱相同的屬性,例如「public TextAlignment TextAlignment {get; set;}」,您不需要完全限定分配給它的枚舉值的名稱空間。但是,Intellisense似乎並不聰明知道這一點。如果沒有資格條件,代碼就會正常工作,你只需要善於注意和避開Intellisense。

+0

你是說Visual Studio 2005中的Intellisense比VS 11中的Intellisense更聰明嗎?我在工作中使用VS2005,我從來沒有遇到過這個問題,而不是一次。 – 2012-04-28 21:37:43

+0

我不知道?自2008年以來我沒有使用過2005年。 – 2012-04-30 02:59:23

0

除了上面的答案,我還有一些涉及Microsoft Office Automation的項目。我的類被構造爲

  • CustomNameSpace.Library.Microsoft.Word.CustomClass1
  • CustomNameSpace.Library.Microsoft.Excel.AnotherCustomClass

當試圖訪問.NET框架本身的微軟命名空間內的任何事情,智能感知會迫使你完全限定的名字。在發生根衝突的情況下,它也會預先設定全局關鍵字,例如:global::Windows.Forms.etc...

相關問題