2015-02-08 48 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