C#将类导入另一个类不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14728422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:50:07  来源:igfitidea点击:

C# importing class into another class doesn't work

c#classmain

提问by Bluefire

I'm quite new to C#, and have made a class that I would like to use in my main class. These two classes are in different files, but when I try to import one into the other with using, cmd says says

我对 C# 很陌生,并且已经创建了一个我想在我的主类中使用的类。这两个类位于不同的文件中,但是当我尝试使用 将一个类导入另一个时using,cmd 说

The type or namespace name "MyClass" could not be found (are you missing a using directive or an assembly reference?

找不到类型或命名空间名称“MyClass”(您是否缺少 using 指令或程序集引用?

I know that in Java I have to mess around with CLASSPATHto get things like this to work, but I have no idea about C#.

我知道在 Java 中我必须弄乱CLASSPATH才能让这样的事情工作,但我不知道 C#。

Additional details:

额外细节:

As you've probably figured out, I'm compiling and executing via command prompt. I'm compiling my non-main class using /target:library(I heard that only main classes should be .exe-files).

正如您可能已经发现的那样,我正在通过命令提示符进行编译和执行。我正在使用/target:library(我听说只有主类应该是 .exe 文件)编译我的非主类。

My code looks like this:

我的代码如下所示:

public class MyClass {
    void stuff() {

    }
}

and my main class:

和我的主要课程:

using System;
using MyClass;

public class MyMainClass {
    static void Main() {
        MyClass test = new MyClass();
        /* Doesn't work */
    }
}

I have tried to encompass my non-main class with namespace MyNamespace { }and importing that, but it doesn't work either.

我试图包含namespace MyNamespace { }并导入我的非主类,但它也不起作用。

采纳答案by Alex

usingis for namespaces only - if both classes are in the same namespace just drop the using.

using仅用于命名空间 - 如果两个类都在同一个命名空间中,只需删除using.

You have to reference the assembly created in the first step when you compile the .exe:

编译 .exe 时,必须引用第一步中创建的程序集:

csc /t:library /out:MyClass.dll MyClass.cs
csc /reference:MyClass.dll /t:exe /out:MyProgram.exe MyMainClass.cs

You can make things simpler if you just compile the files together:

如果您只是将文件编译在一起,则可以使事情变得更简单:

csc /t:exe /out:MyProgram.exe MyMainClass.cs MyClass.cs

or

或者

csc /t:exe /out:MyProgram.exe *.cs

EDIT: Here's how the files should look like:

编辑:以下是文件的外观:

MyClass.cs:

MyClass.cs

namespace MyNamespace {
    public class MyClass {
        void stuff() {

        }
    }
}

MyMainClass.cs:

MyMainClass.cs

using System;

namespace MyNamespace {
    public class MyMainClass {
        static void Main() {
            MyClass test = new MyClass();
        }
    }
}

回答by DHN

Well what you have to "import" (use) is the namespace of MyClassnot the class name itself. If both classes are in the same namespace, you don't have to "import" it.

那么您必须“导入”(使用)的是名称空间而MyClass不是类名本身。如果两个类都在同一个命名空间中,则不必“导入”它。

Definition MyClass.cs

定义 MyClass.cs

namespace Ns1
{
    public class MyClass
    {
        ...
    }
}

Usage AnotherClass.cs

用法 AnotherClass.cs

using Ns1;

namespace AnotherNs
{
    public class AnotherClass
    {
        public AnotherClass()
        {
            var myInst = new MyClass();
        }
    }
}

回答by LukeHennerley

namespace MyNamespace
{
    public class MyMainClass
    {
        static void Main()
        {
            MyClass test = new MyClass();
        }
    }

    public class MyClass
    {
        void Stuff()
        {

        }
    }
}

You have no need for using a namespace then because it is all encompased in the same namespace.

您不需要使用命名空间,因为它都包含在同一个命名空间中。

If you are unsure of what namespace your class is located, type the class (case sensitive you wish to use) then with your cursor on the class, use CTRL+ .and it will offer you a manual import.

如果您不确定您的类所在的命名空间,请输入类(您希望使用区分大小写),然后将光标放在该类上,使用CTRL+.它将为您提供手动导入。

回答by Hossein Narimani Rad

MyClassis a classnot a namespace. So this code is wrong:

MyClass是一个而不是一个命名空间。所以这段代码是错误的:

using MyClass //THIS CODE IS NOT CORRECT

You should check the namespace of the MyClass(e.g: MyNamespace). Then call it in a proper way:

您应该检查MyClass(例如:MyNamespace)的命名空间。然后以适当的方式调用它:

MyNamespace.MyClass myClass =new MyNamespace.MyClass();

回答by gzaxx

usingis used for importing namespaces not classes.

using用于导入命名空间而不是类。

So if your class is in namespace X

所以如果你的类在命名空间中 X

namespace X
{
    public class MyClass {
         void stuff() {

         }
    }
}

then to use it in another namespace where you want it

然后在你想要的另一个命名空间中使用它

using System;
using X;

public class MyMainClass {
    static void Main() {
        MyClass test = new MyClass();
    }
}

回答by RobStevo

If they are separate class files within the same project, then you do not need to have an 'import' statement. Just use the class straight off. If the files are in separate projects, you need to add a reference to the project first before you can use an 'import' statement on it.

如果它们是同一个项目中的单独类文件,则不需要“导入”语句。直接使用类。如果文件位于不同的项目中,则需要先添加对项目的引用,然后才能对其使用“导入”语句。

回答by Guy

If the other class is compiled as a library (i.e. a dll) and this is how you want it, you should add a reference from visual studio, browse and point to to the dll file.

如果另一个类被编译为库(即 dll)并且这是您想要的方式,则您应该添加来自 Visual Studio 的引用,浏览并指向 dll 文件。

If what you want is to incorporate the OtherClassFile.cs into your project, and the namespace is already identical, you can:

如果您想要将 OtherClassFile.cs 合并到您的项目中,并且命名空间已经相同,您可以:

  1. Close your solution,
  2. Open YourProjectName.csprojfile, and look for this section:

    <ItemGroup>                                            
        <Compile Include="ExistingClass1.cs" />                     
        <Compile Include="ExistingClass2.cs" />                                 
        ...
        <Compile Include="Properties\AssemblyInfo.cs" />     
    </ItemGroup>
    
  1. 关闭您的解决方案,
  2. 打开 YourProjectName.csproj文件,并查找此部分:

    <ItemGroup>                                            
        <Compile Include="ExistingClass1.cs" />                     
        <Compile Include="ExistingClass2.cs" />                                 
        ...
        <Compile Include="Properties\AssemblyInfo.cs" />     
    </ItemGroup>
    
  1. Check that the .cs file that you want to add is in the project folder (same folder as all the existing classes in the solution).

  2. Add an entry inside as below, save and open the project.

    <Compile Include="OtherClassFile.cs" /> 
    
  1. 检查要添加的 .cs 文件是否位于项目文件夹(与解决方案中的所有现有类相同的文件夹)中。

  2. 在里面添加一个条目,如下所示,保存并打开项目。

    <Compile Include="OtherClassFile.cs" /> 
    

Your class, will now appear and behave as part of the project. No using is needed. This can be done multiple files in one shot.

您的班级现在将作为项目的一部分出现并运行。不需要使用。这可以一次完成多个文件。

回答by Hyman Gajanan

I know this is very old question but I had the same requirement and just discovered that after c#6 you can use static in using for classes to import.

我知道这是一个非常古老的问题,但我有相同的要求,并且刚刚发现在 c#6 之后,您可以在 using for classes 中使用 static 来导入。

I hope this helps someone....

我希望这可以帮助别人....

using static yourNameSpace.YourClass;