c# - 从不同文件访问 'using' 指令

标签 c#

考虑下一个解决方案结构:

文件 1:

using MyClass = System.Collections.Generic.List<int>;

namespace NamespaceA
{
    class  A
    {
        MyClass a;
    }
}

namespace NamespaceB
{
    class B
    {
        MyClass b;
    }
}

文件 2:

namespace NamespaceC
{
    class C
    {
        MyClass c; // <-- The type or namespace name 'MyClass' could not be found 
    }
}

如何从 File2 访问 MyClass 定义?我试图将它移动到 NamespaceA 并在 File2 中使用 using MyClass = NamespaceA.MyClass,但没有成功。 我的目标是在一个地方定义复杂类型,而不是在所有使用它的文件中。

最佳答案

这称为“使用别名指令”。

Per MSDN:

The scope of a using directive is limited to the file in which it appears.

没有办法做你想做的事;您必须将别名添加到您希望在其中使用它的每个文件。

关于c# - 从不同文件访问 'using' 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871049/

相关文章:

c# - 无法将类型 System.DirectoryServices.AccountManagement.Principal 隐式转换为字符串

c# - 如何摆脱子项 ObjectListView 中的复选框文本

C# 自定义项目模板

c# - 无法访问的函数和方法对 C# 中的性能有多大影响

c# - 如何在 Linux 上的 asp.net 核心中捕获退出信号?

C# .net Core Fluentvalidation 手动验证获取验证器类实例

c# - 如何从以管理员用户身份运行的 C# 代码重新启动 IIS?

C#方法返回一个类型

c# - 将c#日期时间格式转换为java日期时间格式

c# - FileSystemWatcher 两次引发事件