【问题标题】:C# [using] as data annotationC# [使用] 作为数据注释
【发布时间】:2015-04-08 15:53:36
【问题描述】:

我在来自不同类的两个方法之间有一个模棱两可的引用。

这些类位于共享相同结构的不同命名空间中,例如:

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

【问题讨论】:

    标签: c# asp.net-mvc namespaces data-annotations


    【解决方案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!";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      相关资源
      最近更新 更多