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

前端 未结 9 1086
孤街浪徒
孤街浪徒 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:28

    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.

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

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

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

    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.

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

    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

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

    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!

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

    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
    
    0 讨论(0)
提交回复
热议问题