2015-02-08 51 views
0

我在不同類的兩個方法之間有一個模棱兩可的引用。C#[使用]作爲數據註釋

這個類在共享相同的結構就像不同的命名空間:

MyApp.Models.Folder1.Class1 
MyApp.Models.Folder2.Class2 

爲什麼我不能這樣使用?

using MyApp.Models; 
//and use this static method 
Folder1.Class1.StaticMethod(); 

有沒有什麼辦法只爲一個方法使用命名空間?像:

[using(MyApp.Models.Folder1)] 
public ContentResult SomeGetMethod(){ 
    if(Class1.StaticBooleanMethod()) return "nice baby!"; 
    return "that was horrible!"; 
} 

回答

1

爲什麼我不能用這樣的嗎?

MSDN documentation開始,using指令不允許您訪問嵌套在指定名稱空間中的任何名稱空間。

有沒有什麼辦法只爲一個方法使用命名空間?像:

有沒有辦法做到這一點,但是你可以完全限定名稱:

if (MyApp.Models.Folder1.Class1.StaticBooleanMethod()) return "nice baby!"; 

或使用一個別名:

using Folder = MyApp.Models.Folder1; 
... 
if(Folder.Class1.StaticBooleanMethod()) return "nice baby!"; 
+0

絕對是對我的作品的感謝。 – 2015-02-09 00:04:03