如何在 C# 中引用另一个项目?

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

How can I refer to a project from another one in c#?

c#projectprojects-and-solutions

提问by C..

I added a project, Project2, to my solution. It already had another project lets say Project 1. How can I call classes and methods from project2 into project1?

我在我的解决方案中添加了一个项目 Project2。它已经有另一个项目,可以说是项目 1。如何将项目 2 中的类和方法调用到项目 1 中?

What I did:

我做了什么:

I have Project 1 and its solution. I added Project 2 to Project 1's solution. Project 1 and Project 2 both have a namespace named Correction. Now, I called using Correction. However, in Project 1 typing Project2 gives me an error since its claiming that it does not know what it is.

我有项目 1 及其解决方案。我将项目 2 添加到项目 1 的解决方案中。项目 1 和项目 2 都有一个名为 Correction 的命名空间。现在,我调用了使用 Correction。但是,在 Project 1 中键入 Project2 给了我一个错误,因为它声称它不知道它是什么。

I also added project 2 as a reference.

我还添加了项目 2 作为参考。

thanks for all the answers. i dont know what i am doing wrong

感谢所有的答案。我不知道我做错了什么

采纳答案by C..

First, you need to add a reference to Project2 in Project1.

首先,您需要在 Project1 中添加对 Project2 的引用。

If you go to Project1 -> References -> Add Reference, you should see an option to add projects in solutions and add project2.

如果您转到 Project1 -> References -> Add Reference,您应该会看到在解决方案中添加项目并添加 project2 的选项。

Once you add a reference, to call a class Foo in namespace Name1.Name2, you can use the class as

添加引用后,要调用命名空间 Name1.Name2 中的类 Foo,您可以将该类用作

Name1.Name2.Foo foo = new Name1.Name2.Foo(...);

or if you would like to avoid typing, you could add the using statement near the top of the file

或者,如果您想避免输入,可以在文件顶部附近添加 using 语句

using Name1.Name2;

and can now reference the class using just Foo, like

并且现在可以仅使用 Foo 来引用该类,例如

Foo foo = new Foo(...);

Note, you will have figure out the namespacesin Project2. Just using the name Project2 won't work. Look at the file which contains the declaration of the class you want to use and look for the namespace definition.

请注意,您将了解Project2 中的命名空间。仅使用名称 Project2 是行不通的。查看包含要使用的类的声明的文件并查找命名空间定义。

So if you see something as

所以如果你看到一些东西

namespace Name1.Name2 {


    class Bar {
        // Blah
    }

    // Notice the word public here.
    public class Foo {
        // Blah
    }
}

Name1.Name2 is the namespace for Foo and that is what you need to use.

Name1.Name2 是 Foo 的命名空间,这就是您需要使用的。

Also note that you will likely need to have the publicaccess modifier for the class which you want to use in Project1. For example in the above scenario, you should be able to access class Foo, but not class Bar.

另请注意,您可能需要拥有要在 Project1 中使用的类的公共访问修饰符。例如在上面的场景中,您应该能够访问类 Foo,但不能访问类 Bar。

This page will help you understand namespaces: http://www.csharp-station.com/tutorials/lesson06.aspx

此页面将帮助您了解命名空间:http: //www.csharp-station.com/tutorials/lesson06.aspx

回答by Vlad

You need to add Project2 to the references of the Project1. Now you can access the classes from Project2. Don't forget to add the correct namespace!

您需要将 Project2 添加到 Project1 的引用中。现在您可以从 Project2 访问这些类。不要忘记添加正确的命名空间!

回答by James

You just need to add a reference in Project1 to Project 2. You do this by right-clicking the Referencesfolder from the solution explorer pane then you can use the Browseoption to find Project2. Or if it is already added to the solution you can just use the Projectstab.

您只需要在 Project1 中添加一个对 Project 2 的引用。您可以通过右键单击References解决方案资源管理器窗格中的文件夹来完成此操作,然后您可以使用该Browse选项来查找 Project2。或者,如果它已经添加到解决方案中,您可以只使用该Projects选项卡。

Just to clear this up for you. Adding a project to the Solutionis notthe same as adding a reference. Open up Project2 in Visual Studio. Then either add Project1 to the solution aswell or right click on the Referencesfolder in Project2 and add a reference to Project1. To ensure you have properly added a reference expand the references folder and verify you can see Project1in the list.

只是为你解决这个问题。将项目添加到Solution一样加入了参考。在 Visual Studio 中打开 Project2。然后将 Project1 添加到解决方案中,或者右键单击ReferencesProject2 中的文件夹并添加对 Project1 的引用。为确保您已正确添加参考,请展开参考文件夹并验证您是否可以Project1在列表中看到。

Example

例子

Create a new console application and call it MyApplication. Then right click on the Solutionand select the Add New Projectoption and create a new library project and call it MyLib. At this point you have simply added 2 projects to the 1 solution, no references between each project have been created.

创建一个新的控制台应用程序并调用它MyApplication。然后右键单击Solution并选择该Add New Project选项并创建一个新的库项目并调用它MyLib。此时,您只是将 2 个项目添加到 1 个解决方案中,尚未创建每个项目之间的引用。

Right click the Referencesfolder under the MyApplicationproject and select Add Reference.... As MyLibis already part of the solution you can go to the Projectstab and select MyLibfrom the list which creates a new reference to this project in MyApplication. If it is not part of the solution you can use the Browsetab and find the project via explorer.

右键单击项目References下的文件夹MyApplication并选择Add Reference...。由于MyLib已经是解决方案的一部分,您可以转到Projects选项卡并MyLib从列表中进行选择,该列表在MyApplication. 如果它不是解决方案的一部分,您可以使用该Browse选项卡并通过资源管理器查找项目。

So at this point we have established a reference inside MyApplicationto MyLib. So in order to use the classes from MyLibinside MyApplicationwe can either declare a using for the project inside the unit or we can use the full path directly e.g.

所以此时我们已经在里面建立了一个MyApplicationMyLib. 所以为了使用MyLib内部的类,MyApplication我们可以为单元内的项目声明一个 using 或者我们可以直接使用完整路径,例如

// main code file in MyApplication

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyLib;  // This will allow me to access the classes inside MyLib directly

namespace PdfPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
             // if we have declared the namespace at the top, we can do:
             MyLibClass cls = new MyLibClass();
             // or if you don't want to add the namespace at the top we have to do:
             MyLib.MyLibClass cls = new MyLib.MyLibClass();
        }
    }
}

Hope that clears it up a bit for you.

希望这能让你明白一点。

回答by Jamie Keeling

Select the project you will be using (project1 for example), right click it in the solution explorer and click "Add reference".

选择您将使用的项目(例如 project1),在解决方案资源管理器中右键单击它,然后单击“添加引用”。

You'll be able to add a reference to the other solution from there (project2).

您将能够从那里添加对其他解决方案的引用(project2)。

All you have to do then is add a using statement in your main project (project1) and you'll be able to access it normally.

然后你所要做的就是在你的主项目(project1)中添加一个 using 语句,你就可以正常访问它了。

回答by bakasan

You have to specify the "path" to the code you're trying to call. You accomplish this by either

您必须指定要调用的代码的“路径”。您可以通过以下任一方式完成此操作

1) Specify the namespace of the second project/class you wish to use, typically at the top of your code file.

1) 指定要使用的第二个项目/类的命名空间,通常位于代码文件的顶部。

using MySecondProject;

var foo = new ClassFromSecondProject();

2) Explicitly specifying the name of the class you wish to use, including its namespace

2) 明确指定要使用的类的名称,包括其命名空间

//do stuff
var foo = new MySecondProject.ClassFromSecondProject();
//do more stuff

回答by Tom

I just had this very same problem. I triple checked everything, and references were added, project was in the same solution, everything set. I also know my namespaces, I even got code completion but when building, I got the same error that the namespace cannot be found.

我刚刚遇到了同样的问题。我三重检查了所有内容,并添加了引用,项目在同一个解决方案中,一切都设置好了。我也知道我的命名空间,我什至完成了代码,但是在构建时,我遇到了同样的错误,即找不到命名空间。

It took me one hour to find this out: The project I added, that didn't find the respective namespace, was set to a different .NET target framework. I changed my solution to the "full" .NET Framework 4 instead of the Client Profile 4, but the new project I added was set to the Client Profile again.

我花了一个小时才发现:我添加的项目没有找到相应的命名空间,被设置为不同的 .NET 目标框架。我将我的解决方案更改为“完整的”.NET Framework 4 而不是 Client Profile 4,但我添加的新项目再次设置为 Client Profile。

Instead of giving me a proper warning about the bad configuration, it claimed that namespaces could't be found.

它没有给我一个关于错误配置的正确警告,而是声称找不到命名空间。

Hope this helps someone, and saves you the time that I just wasted.

希望这对某人有所帮助,并为您节省我浪费的时间。

回答by Per

As stated by others, you need to add the (existing) projects to the solution. Then you need to add a reference to them under Solution -> Projects. They should now show up with their namespaces in the Object Browser.

正如其他人所说,您需要将(现有)项目添加到解决方案中。然后您需要在Solution -> Projects下添加对它们的引用。他们现在应该在对象浏览器中显示他们的命名空间。

There is one more thing you need to make sure, which took me some experimentation to find out. Whenever I added my second project, the first one stopped working, and the error message was the same as when the project misses a reference ("The type or namespace name 'Ber' could not be found (are you missing a using directive or an assembly reference?)"). The problem was that both projects had the same default Assembly name("Class Library") in project Properties -> Application. So give them unique names. I use the same as the Default namespace.

还有一件事你需要确保,我花了一些实验来找出答案。每当我添加我的第二个项目时,第一个项目停止工作,并且错误消息与项目缺少引用时相同(“找不到类型或命名空间名称 'Ber'(您是否缺少 using 指令或程序集参考?)”)。问题是这两个项目在项目Properties -> Application 中具有相同的默认程序集名称(“类库”)。所以给他们独特的名字。我使用与Default 命名空间相同的名称

回答by Maryadi Poipo

I had the same problem with this error. I couldn't detect project1 from project2. I had added reference from project2 to project1 but it still didn't work. Then I unloaded project1, removed it from the solution. Then I added it again, made reference from project2 and ta da... it worked..... :-)

我对这个错误有同样的问题。我无法从 project2 中检测到 project1。我已经添加了从 project2 到 project1 的引用,但它仍然没有用。然后我卸载了 project1,将其从解决方案中删除。然后我再次添加它,从 project2 和 ta da 中引用...它有效..... :-)

回答by DaveDean1

I don't have enough rep to upvote Tom's answer but it helped me greatly. In addition, you must select the same subversion of the .NET Framework(in addition ot it being the same full/core/standard etc). I had 4.5 specified in a testing project and got this error, changing it to 4.6.1 (the same as the others) fixes this issue.

我没有足够的代表来支持汤姆的回答,但这对我帮助很大。此外,您必须选择.NET Framework相同子版本(此外,它是相同的完整/核心/标准等)。我在测试项目中指定了 4.5 并收到此错误,将其更改为 4.6.1(与其他版本相同)修复了此问题。