2013-02-15 80 views
1

我有我的解決方案庫中的項目,我只是說,每當我需要引用類/枚舉我必須使用:使用問題,爲什麼我必須使用使用global :: MyName.SubName?

using global::MyName.SubName;

(ReSharper的自動提示這一點)

爲什麼這是?爲什麼我不能這樣做:

using MyName.SubName; 

哪一個不行。甚至當它這樣做,我得到這個錯誤:

Error 400 Member 'MyName.SubName.Enumerations.SomeEnum.SomeEnumName1' cannot be accessed with an instance reference; qualify it with a type name instead 
+0

一個更有意義的語法,你可能有一些不確定性,因爲一個或多個具有相同名稱的類型與其他命名空間。你使用了什麼其他的命名空間? – 2013-02-15 17:11:54

+0

@ user1361315我的評論有幫助嗎?這有沒有解決? – Kelsey 2014-01-22 19:37:08

回答

0

我只能假設你MyName.SubName是HID(同名)的東西。

詳情參見下面的MS文章:How to: Use the Global Namespace Alias

我認爲這說明你碰到的場景。

The ability to access a member in the global namespace is useful when the member might be hidden by another entity of the same name.

...

However, in larger projects, it is a very real possibility that namespace duplication may occur in one form or another. In these situations, the global namespace qualifier is your guarantee that you can specify the root namespace.

+0

所以我有2個項目:MyProject.Web和我創建的新項目是MyProject.Common。 MyProject.Web中沒有任何其他名稱空間中包含「common」一詞,因此很困惑。當我探索代碼/名稱空間時,我也沒有看到任何衝突。 – loyalflow 2013-02-15 20:00:56

+0

@ user1361315您可能與框架中的其他內容衝突。編譯器無法弄清楚你指的是什麼,所以這應該是原因。靜態類/方法也可以創建類似的行爲,但我認爲這不是這種情況。 – Kelsey 2013-02-15 21:27:00

0

我同意@kelsey,它看起來像一個命名空間衝突。解決它的另一種方法是使用命名空間別名,如;

using MNSub = MyName.SubName 

// ...else where in code 
MNSub.MyType varName = new MNSub.MyType(); // etc. 

這至少給比global::...

相關問題