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

前端 未结 9 1087
孤街浪徒
孤街浪徒 2020-12-01 12:13

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

相关标签:
9条回答
  • 2020-12-01 12:36

    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.

    0 讨论(0)
  • 2020-12-01 12:39

    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.

    0 讨论(0)
  • 2020-12-01 12:52

    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.

    0 讨论(0)
提交回复
热议问题