简体   繁体   中英

How can i add a new project to my existing already project and create a dll file from the new project?

I already added a new project to my project in the new project I just created a new class and copied a code to the new class a code which I want to use in the other project. The new project is also a Windows Forms type.

What I want is to create a dll from the new project class so I can later use the dll in the other project to use the class in the dll from the new project I just added.

So in the new added project I changed in the project properties>application>Output Type>Class Library But now when I'm trying to add the reference of the new project class I can't find any dll file in the new project under bin>Debug directory.

Again I have one project which I added a new project to it's solution. The new project is also a windows forms type without any code but only one added class which there I added some code. What I want to do is to create a dll of the new project so I can use the class later with the main project.

But why it didn't create any dll?

The short answer would be that maybe you didn't compile it yet?

However, there's a better answer. If I understand you correctly, you have this:

Solution 1
  -- Windows Forms Project A
  -- Windows Forms Project B

And there is code in Windows Forms Project B which needs to be accessed by Windows Forms Project A . You took a step in the right direction here:

So in the new added project i changed in the project properties>application>Output Type>Class Library

You might want to take an extra step to clean that up, though. Because now your Class Library Project B is still referencing Windows Forms libraries that it doesn't need. In fact, it very much shouldn't reference Windows Forms libraries or any other UI libraries. In order to be portable, the Class Library should be entirely UI-independent. (As a tip, if you ever find yourself needing to reference a UI library from a portable Class Library then you probably made a mistake somewhere and should re-think what logic belongs where.)

So, for starters, create a brand new Class Library project so that you have this:

Solution 1
  -- Windows Forms Project A
  -- Class Library Project B

Next, you need Project A to reference Project B . You're trying to do this by referencing the output DLL, but this isn't the right approach unless you have a very good reason to do this. Instead, since they're in the same Solution you can use a Project Reference .

If you're using Visual Studio, right click on Project A and select Add Reference . Depending on the version of Visual Studio, the layout will be different. But what you want to add is a Project Reference to another Project in the Solution. Select Project B and you're all set.

At this point, you don't need to worry about the compilation of Project B independently. It's merely a component referenced by Project A . When you compile Project A , a DLL for Project B will be in the compiled output by default.

EDIT: If you're not finding the DLL it's probably because it hasn't been built yet. Besides, explicitly adding the DLL when the 2 projects are in the same solution probably isn't what you want to do anyway.

Continuing with my original post: Instead of browsing for the DLL, click on the "Projects" tab on the "Add Reference" dialog and it will add the reference from there.

When you add the new project to your solution, you need to specify a Class Library, not a Windows Forms Application. Once that is done, you will be able to add a Reference to your first project, and the second project (the library) should be an option in one of the tabs (possibly "Projects" or "Solution" depending on your version). The DLL will not show up until you actually build the Class Library project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM