gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:36:07 25 4
gpt4 key购买 nike

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

文件 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com