C# 如何从另一个项目中的一个项目调用类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11383181/
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-09 17:40:35  来源:igfitidea点击:

How to call classes from one Project in another Project?

c#.netvisual-studioconsole-applicationproject-reference

提问by 13driver

Excuse the incredibly silly question but im new to C# . I just can't figure out how to use classes from one Project in another Project.

请原谅这个非常愚蠢的问题,但我是 C# 新手。我只是不知道如何在另一个项目中使用一个项目中的类。

Lets say I want to take a string from Project1 to Project2 and have Project2 print said string .

假设我想将一个字符串从 Project1 带到 Project2 并让 Project2 打印该字符串。

I reference Project2 from Project1 with the “add reference” menu , then I add “using Project2” and then I write this to trying and call "print" from "ClassA" in "Project2".

我使用“添加引用”菜单从 Project1 中引用 Project2,然后添加“使用 Project2”,然后将其写入尝试从“Project2”中的“ClassA”调用“print”。

        Project2.ClassA Classa = new Project2.ClassA();
        Console.WriteLine(Classa.print);

but all i get are Error messages .

但我得到的只是错误消息。

so can anyone please give a step by step explanation of EXACTLY why I need to do ?

那么任何人都可以一步一步地解释我为什么需要这样做吗?

回答by Gerald Versluis

When you reference the Class from Project2 it is probably in a different namespace.

当您从 Project2 引用 Class 时,它可能位于不同的命名空间中。

Add the namespace in the top of your class where you are going to use is (the using statements) or, go with your cursor over the Project2.Class and let Visual Studio do it for you :-)

在您将要使用的类的顶部添加命名空间(using 语句),或者将光标移到 Project2.Class 上,让 Visual Studio 为您完成:-)

回答by Tilak

Make Classa public, and add assembly reference/project reference of Project1 to Project2.

公开Classa,并将Project1的程序集引用/项目引用添加到Project2。

How to add assembly reference

如何添加程序集引用

回答by Khalid

Follow these instructions:

请按照以下说明操作:

  1. In the Solutions Explorer, right click on the project that you need to refer from.

  2. Select "Add Reference". (latter versions i.e. beyond 2015; not exactly sure though; it should be "Add->Reference". Right click on Referencesand select Add Referencealso will do.)

  3. Reference Manager - ProjectNamedialog will appear.

  4. In the left pane, expand Projectsmenu. That will populate a list of existing projects in the middle of the dialog.

  5. Tick the checkbox before each project that you need to refer to.

  6. Press OK.

  1. 在解决方案资源管理器中,右键单击您需要引用的项目。

  2. 选择“添加引用”。(较晚的版本,即 2015 年以后;虽然不确定;它应该是"Add->Reference"。右键单击References并选择Add Reference也可以。)

  3. 将出现参考管理器 - 项目名称对话框。

  4. 在左窗格中,展开“项目”菜单。这将在对话框中间填充现有项目的列表。

  5. 勾选您需要参考的每个项目前的复选框。

  6. 确定

Once done, you can use all the public and protected (given you are inheriting from an existing one) in the project that you have referred to. Make sure you add using NameSpace.Classon the imports list.

完成后,您可以在您引用的项目中使用所有公共和受保护的(假设您是从现有项目继承的)。确保您添加using NameSpace.Class到导入列表中。

回答by Digvijay Rathore

Go to the project Say P2, expand it go to references right click add reference. Go to project Tab then browse for the Project P1 and click add. After that you would need a using statement for that project to get the files. Click on rebuild solution it will give you the output.

转到项目 Say P2,将其展开转到引用右键单击添加引用。转到项目选项卡,然后浏览项目 P1 并单击添加。之后,您需要为该项目使用 using 语句来获取文件。单击重建解决方案,它将为您提供输出。

回答by Romeo Sierra

Follow these instructions:

请按照以下说明操作:

  1. In the Solutions Explorer, right click on the project that you need to refer from.

  2. Select "Add Reference". (latter versions i.e. beyond 2015; not exactly sure though; it should be "Add->Reference". Right click on Referencesand select Add Referencealso will do.)

  3. Reference Manager - ProjectNamedialog will appear.

  4. In the left pane, expand Projectsmenu. That will populate a list of existing projects in the middle of the dialog.

  5. Tick the checkbox before each project that you need to refer to.

  6. Press OK.

  1. 在解决方案资源管理器中,右键单击您需要引用的项目。

  2. 选择“添加引用”。(较晚的版本,即 2015 年以后;虽然不确定;它应该是"Add->Reference"。右键单击References并选择Add Reference也可以。)

  3. 将出现参考管理器 - 项目名称对话框。

  4. 在左窗格中,展开“项目”菜单。这将在对话框中间填充现有项目的列表。

  5. 勾选您需要参考的每个项目前的复选框。

  6. 确定

Once done, you can use all the public and protected (given you are inheriting from an existing one) in the project that you have referred to. Make sure you add using NameSpace.Classon the imports list.

完成后,您可以在您引用的项目中使用所有公共和受保护的(假设您是从现有项目继承的)。确保您添加using NameSpace.Class到导入列表中。