Reputation: 2860
I am trying to create a dll of one of the many class files in my project (which is an application) so that it can be used by other programming languages. I can't isolate this from the rest of the project because it is dependent on certain parts of it. My question is how can I get this one cs file to be compiled into a DLL along with all of the necessary namespaces included that I say "using" at the top? Thanks
Upvotes: 2
Views: 227
Reputation: 4366
You can create a Class Library which when you "debug" creates the .dll
file for you. Follow the tutorial here to learn how to make one. If it is already in your project go to
your solution -> add project -> new -> and select a "Class library"
then in your project go to resources -> add -> projects -> your class library name
or
resources -> add -> browse -> your class library location
Hope this helps!
Upvotes: 0
Reputation: 931
Step 1 Right click solution -> Add -> New Project -> Name it -> Add it.
Step 2 While holding shift drag the cs file you want to move into the new project. This will move it without leaving a copy in the original project
Step 3 In the original assembly right click Refereces -> Add Reference -> From the projects tab select the newly added project.
Step 4 When you want to access the moved assembly, go to the top of the page and write using followed by the namespace of your generic assembly.
It's a great idea to try to reuse code in multiple applications. Once you follow these steps and build the application, the newly added project will compile into a separate dll. However, you still will not be able to use that dll in other applications if it relies on code in your original project. If you plan to reuse a section of code, it is a good idea to plan ahead and write it in as generic a manner as possible. Try to place your application specific logic in one assembly (project/dll) and the generic logic in the second assembly (project/dll) then have them communicate by referencing the project and adding a using statement at the top of the file you want to access the dll from.
Upvotes: 0
Reputation: 8397
If you have a piece of code that you are using in many different applications, you should move that into a separate assembly. Create a new Project (File -> New Project -> Class Library) and have that code live there. You can then reference that DLL from any other solution you need it in.
You can also add a project to an ongoing solution and do the same thing (right click Solution -> Add -> New Project -> Class Library). Then just reference the project itself (right click References -> Solution -> pick the project). The exact menu choices differ a bit between VS2010 and 2012, but I think you get the general idea.
This act of reusing your code without copying it is one of the basic practices of DRY coding - never copy and paste code. Always refactor so that you can reference the same piece of code in two completely different places. If you don't employ this technique, you will eventually end up with two different versions of the "same" code, which is a maintenance nightmare.
Upvotes: 3
Reputation: 18843
are you looking to use csc.exe if so you could follow something like this example be sure to replace the names with your own .cs file name(s) I will also include a link below for MSDN to explain how to use csc.exeCommand-line Building With csc.exe
csc /t:library source.cs -> source.dll
Upvotes: 0