How to compile cs files into separate dll - c#

I have a class library and cs files which are for different objectives. One is extension class, the other is windows form control the other is asp.net control, etc.
I want to compile all these cs files into a different dll.
PS: Some of them will need more than one class files maybe.

You may try command line compilation. (Working with the C# 2.0 Command Line Compiler)
csc /target:library /out:Something.xyz *.cs

I know this sounds too obvious, but if you want to compile them into seperate DLLs, why don't you create a project per assembly? So that's a project for the extension classes, one for the asp.net controls etc...

You have to create different projects in your solution (assuming you work in Visual Studio).
Each project can have multiple (class, resource, form, etc.) files and will be compiled into different assemblies (dll's). For each project you can specify settings (assembly name, target framework, etc.).
Classes from different projects can "use" each other by making references from one project to the other. Also, different projects can specify the same namespaces so that you can structure the aplication to your own wishes.
See Structuring Solutions And Projects

All *.cs in a single class library project will compile into the same DLL, you can not split them into individual dlls. If you want a seperate dlls for each class then each should be in a seperate class library project.

Related

How to merge a subset of linked dll files with one exe file (C#, WinForms)?

I have a solution with two C# projects: One WinForms application (exe) and one library (dll). Multiple external dll files are referenced. I would like to merge the output of my projects into one exe file, while keeping the other dll files separately, i.e. instead of
myApp.exe
myLib.dll
externalLib1.dll
externalLib2.dll
...
I would like to have
myMergedAppAndLib.exe
externalLib1.dll
externalLib2.dll
...
Is this actually possible?
I am aware of the ILMerge tool, but it seems to be deprecated and I don't know whether it is ok not to include all referenced dll files.
I am also aware of the publishing option in Visual Studio Produce single file, but to my understanding this will also merge the external dll files, right?
I cannot merge the two projects, because the library project is also used for a third project (another WinForms exe).
Related SO question: merge-dll-into-exe

How to create .dll file from multiple files with different extension

Can I create a single dll file of my project which contain other dependent dlls and some other subfolders which contains file which are not .cs file but use to make calculation in my project.
My project look like this:
Above image first arrow shows reference which comes after adding dll ex. abc.dll.
And arrows 2 and 3 shows folders in my project which contains file which are used in code. And those are not .cs (C# files).
Is it possible to create single dll which contain all this dlls and files in folders so that I can distribute to anyone.
Or is there any other way I have to follow.
Let me suggest
You can use ILMerge to solve combining multiple DLL's into one, found here: http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx also found in nuget. For the other items, you can add them as resources: https://support.microsoft.com/en-us/kb/319292
Yes you can make a single interface (ONE DLL) that is using other DLL and your own source code.
But you need to ship other DLLs too with your DLL. Because without it other people will get error when consuming other DLL functions.
You'll have to package up all the dependencies as resources in your project.

How do I build a multiple project solution in Visual Studio without dependencies between the binaries?

I'm using Visual Studio 2010 Pro to build a solution that contains two projects. Project A contains most of my source code, while Project B is intended to run independently, but must use some of the source code contained in Project A.
Under the current configuration, Project A is contained as a reference within Project B. I'd like to be able to build and maintain versions of each project independently, but it appears that when I build the entire solution, ProjectB.exe cannot run without ProjectA.exe in the same local directory. I would think and hope that when the .exe binaries are compiled that all of their dependencies are packaged within each, but that appears not to be the case. In fact, any attempt to run ProjectB.exe while ProjectA.exe is not present results in a System.IO.FileNotFoundException.
Is there a way to build a version ProjectB.exe that runs independently and avoids code duplication?
In cases where you want common code, the best solution is to break out the common classes into a third assembly to serve as a library. (As per Adriano's suggestion.) The other option he hints at is to use the "as link" option when using the "add existing file" to the second project.
If you don't know where it is, use the "Add existing file" option, then in the dialog box to select the file, the "Add" button has a drop-down selection where you can select "As Linked File" (or something to that effect.)
This allows you to compile the same classes into multiple projects. But keep in mind that the namespacing for the linked file cannot be changed for the second project. If the namespace was "ProjectA.Domain", this is how you need to access it in Project B. This was a useful trick for Silverlight projects back before the multi-platform assemblies were introduced.
If you want to get rid or the dependency on A, you will have to extract the common logic into another project (let's call it C), as Adriano suggested in a comment.
If you need even looser bond between the projects, you can reference A (or C) not as a project, but as a built assembly (.dll file) and check Specific Version reference property to True. Additionally, if your project/codebase structure is more complex, check more assembly sharing options here.
Some options:
The common option: Separate the common code into a third class library (DLL) project. And have both ProjectA and ProjectB dependent on it. The downside is that now in order to run the projects you need two files (the main exe and the dll.) This method is how most software is developed: a single executable and a bunch of DLLs.
The correct option: Separate the common code into a third project and modify the project files to create executables that contain both assemblies (similar to statically linked libraries in unmanaged code.) The downside is that Visual Studio does not support this out of the box and you need to modify the project files which are actually MS-Build definition files to do this.
The ugly option: Create shortcuts for the common files in ProjectA in ProjectB. This is the same as copying the common code to the other project, but you're still left with one source file. The downside is that you have to do this for every file and maintain the same structure in both projects. This is an ugly, if viable, option. Choose one of the others.

Reference other project in solution without separate dll

At the moment I have added and referenced another project to my main solution. It works, but then I need the separate dll that is compiled.
How can I reference another project without the need for a separate dll that I have to distribute with my final exe?
A C# class project in a Visual Studio solution always compiles to an assembly, so adding a project reference will inevitably mean that your executable references the assembly built as a result of compiling the other project. If you don't want to distribute the separate assembly with your executable the only thing you can do is to ILMERGE the assembly into your executable as a part of your deployment build process,
You can't. The way .net works is to load an additional assembly and using its meta data the classes inside that. If you are attempting to have a set of classes that you refer to by source similar to the way c++ uses header files you will have to import those files into your project.
This sounds like you're trying to statically compile a library. This is not (easily) offered by .NET without manually creating and merging assemblies.

How to embed localizations in .NET assemblies

I created a .NET class library in C# with some resources for localization and already translated them to different languages and put them into different *.resx-files.
But when I compile the assembly, only the default *.resx-files are compiled into the resulting dll and the others are compiled into different dlls in different sub folders.
I know this is the default behavior of Visual Studio but for me this is not very useful, because I do not want to distribute many files in many folders but just one independent dll.
So I need to know what I have to change to compile everything into one dll.
You can use IL Merge to combine multiple assemblies into one .dll file.

Categories