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

故事扮演 提交于 2019-12-17 09:41:46

问题


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?

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.

I also added project 2 as a reference.

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


回答1:


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

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

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

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 Name1.Name2;

and can now reference the class using just Foo, like

Foo foo = new Foo(...);

Note, you will have figure out the namespaces in 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.

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.

Also note that you will likely need to have the public access 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.

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




回答2:


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.

using MySecondProject;

var foo = new ClassFromSecondProject();

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

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



回答3:


You just need to add a reference in Project1 to Project 2. You do this by right-clicking the References folder from the solution explorer pane then you can use the Browse option to find Project2. Or if it is already added to the solution you can just use the Projects tab.

Just to clear this up for you. Adding a project to the Solution is not the same as adding a reference. Open up Project2 in Visual Studio. Then either add Project1 to the solution aswell or right click on the References folder 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 Project1 in the list.

Example

Create a new console application and call it MyApplication. Then right click on the Solution and select the Add New Project option 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.

Right click the References folder under the MyApplication project and select Add Reference.... As MyLib is already part of the solution you can go to the Projects tab and select MyLib from the list which creates a new reference to this project in MyApplication. If it is not part of the solution you can use the Browse tab and find the project via explorer.

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

// 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.




回答4:


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.

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.




回答5:


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

You'll be able to add a reference to the other solution from there (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.




回答6:


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.




回答7:


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!




回答8:


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.

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.




回答9:


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..... :-)



来源:https://stackoverflow.com/questions/2306652/how-can-i-refer-to-a-project-from-another-one-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!