Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问';使用';来自不同文件的指令_C# - Fatal编程技术网

C# 访问';使用';来自不同文件的指令

C# 访问';使用';来自不同文件的指令,c#,C#,考虑下一个解决方案结构: 文件1: using MyClass = System.Collections.Generic.List<int>; namespace NamespaceA { class A { MyClass a; } } namespace NamespaceB { class B { MyClass b; } } namespace NamespaceC { class

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

文件1:

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

namespace NamespaceA
{
    class  A
    {
        MyClass a;
    }
}

namespace NamespaceB
{
    class B
    {
        MyClass b;
    }
}
namespace NamespaceC
{
    class C
    {
        MyClass c; // <-- The type or namespace name 'MyClass' could not be found 
    }
}
using MyClass = System.Collections.Generic.List<int>;

namespace NamespaceC
{
    class C
    {
        MyClass c; // Should work now.
    }
}
使用MyClass=System.Collections.Generic.List;
名称空间
{
甲级
{
我的a级;
}
}
名称空间
{
B类
{
MyClass b;
}
}
文件2:

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

namespace NamespaceA
{
    class  A
    {
        MyClass a;
    }
}

namespace NamespaceB
{
    class B
    {
        MyClass b;
    }
}
namespace NamespaceC
{
    class C
    {
        MyClass c; // <-- The type or namespace name 'MyClass' could not be found 
    }
}
using MyClass = System.Collections.Generic.List<int>;

namespace NamespaceC
{
    class C
    {
        MyClass c; // Should work now.
    }
}
名称空间c
{
C类
{
MyClass c;//这称为“使用别名指令”

using
指令的范围仅限于它出现的文件


无法执行您试图执行的操作;您必须将别名添加到您希望在其中使用它的每个文件中。

您必须将using添加到您希望在其中使用它的每个文件中

文件2:

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

namespace NamespaceA
{
    class  A
    {
        MyClass a;
    }
}

namespace NamespaceB
{
    class B
    {
        MyClass b;
    }
}
namespace NamespaceC
{
    class C
    {
        MyClass c; // <-- The type or namespace name 'MyClass' could not be found 
    }
}
using MyClass = System.Collections.Generic.List<int>;

namespace NamespaceC
{
    class C
    {
        MyClass c; // Should work now.
    }
}
使用MyClass=System.Collections.Generic.List;
名称空间
{
C类
{
MyClass c;//现在应该可以工作了。
}
}

这是当前文件中的一个别名,而不是在其他任何地方。如果允许的话,请考虑混乱,当您应该直接使用类型或生成子类(如果您需要额外的逻辑)时,您在使用别名。“我的目标是在一个地方定义复杂类型,而不是在使用它的所有文件中定义复杂类型。”听起来您应该在某个名称空间中定义这些类型,并引用该名称空间。是的,这是我的疏忽。谢谢!