Creating DLL files in C# - c#

When creating DLL files for a program that already exists, is it customary to create them by going to
File >
New >
Project >
Class Library,
File >
Add >
New Project >
Class Library,
or
File >
Add >
Existing Project >
Class Library?

You should differentiate 2 things:
DLLs - compiled code in machine (in case of .net, clr) readable format for execution
Code - source code files that get compiled into DLLs.
If you have a dll that you can use, you add it as a reference to your project.
If you have source codes, you can add them the way you specified (add existing project). If you want to write new .dll, you should use "Create new class library". Note that whenever you add through "Add existing project", project (along with source codes) isn't copied to your solution folder.
Basics you should know before you go on:
Solution - means to tie several projects together into one logical bunch.
Project - means to tie several source code files/resources/etc. into one logical bunch that gets compiled into one physical unit - dll/exe/etc.
Source file - code file like MyClass.cs. This is where the code is written.
Reference - reference from one project to another one in order to obtain/use public-visible functionality.
I'd recommend reading some books on C#/.NET to get clearer understanding.

You are right, that is one way of creating dll,(for a class library in visual studio, you will get a dll) but its lot more than that.
You should gather some knowledge for DLL check out this link
http://msdn.microsoft.com/en-us/library/1ez7dh12.aspx
Also check out this link how to create dll in c#
http://msdn.microsoft.com/en-us/library/3707x96z(v=vs.80).aspx

Well what you have mentioned in your question is a good and simple way of creating DLL with C# (that is with Class Library project) , further more you can direct your Compiler (csc.exe , it the Csharp compiler) with some commands to make a DLL for you,
Consider that you have few classes such as Add.cs (can Add numbers) Mult.cs (can multiply)
To build the file MathLibrary.DLL, you can use command like this
csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
The /target:library compiler option tells the compiler to output a DLL instead of an EXE file.
The /out compiler option followed by a file name is used to specify the DLL file name.
P.S: Solution derived from How to: Create and Use C# DLLs
Referencing Custom Made DLL in C# Projects:
Add a reference of the DLL
Add namespace in you project (or just start using DLL by fully qualified name)
Snapshots

Related

Converting a C# class I created inside a project to a separate reusable class

I'm not new to C# programming, but I suppose I'm new to programing "the right way" in C#. I've worked in C on embedded devices for years and have written desktop apps to support them. First in VB6, then in C#.
I recently started making better use of classes for reusing code (and for instantiating more than one instance of the class in a program). For example, I "wrapped" a UART interface with some additional functionality so I can use the same code for multiple ports by creating an instance of the class for each one.
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
I'm sure there's a way to create it such that I can just reference it like everything else with either a "using..." reference at the top of the program or with a "Project | References..." checkbox. But for the life of me I can't find a good learning journey for this.
Any direction would help.
You want to create your reuseable class in an assembly - this is the equivalent of a dll from your C experience.
To create an assembly, have a separate project of type assembly (instead of exe) . You can reference the assembly from other projects. If your project is in the same solution you can reference the project, otherwise you can reference the compiled assembly.
C# uses a packaging system called Nuget, so you can package your assemblies into "Nugets" which you host in a Nuget Server. You can then use tooling to discover and import these.
Please create a Class Library project and include your class into that project. Make sure your class is public. Once you build this project you'll get an assembly which can be referenced from other projects. See Tutorial: Create a .NET class library using Visual Studio
There are different ways of referencing it.
You can have the class library project in the same solution as the main project. In this case you should add a project reference.
You can copy the compiled *.dll file to some folder in your solution (e.g. Lib) and add an assembly reference.
If this assembly is to be used in multiple projects please consider creating a NuGet package with this library and pushing it to some repository. Then other projects can add a package reference to this package.
Details:
How to: Add or remove references by using the Reference Manager
Install and manage packages in Visual Studio using the NuGet Package Manager
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
Well, it isn't the best practice but (unfortunatly) still a common behavior. So don't worry to much about it.
What you could do to improve it place the file (and other reusable parts) in a seperated csproj.
For example name the project of the type class library and name it VinDag.Tools. Within the project create a folder UART and place the wrapper there. The namespace of the wrapper would then be VinDag.Tools.UART.
From know on you can just reference the class library instead of renaming the file. It's not necessarily required to be the same namespace as the project.
From there you can start considering (private) nugets. This would prevent you from copying files/csproj around.

Same Assembly Version in multiple mixed .NET projects (same solution)

I've got a problem with setting up one "Shared" assembly info in my solution. I need one place to change assembly version for every project in the solution (currently > 25 project).
The problem is, that the solution contains C# projects as well as VB.NET projects - because of that, I am not able to create ONE file with assembly version (I cannot include C# files in VB.NET project and vice versa...)
Is there any way to solve the problem ?
You can use a Shared Assembly Info using file linking.
Here is a nice article from Ashish Jain:
https://weblogs.asp.net/ashishnjain/sharing-assembly-version-across-projects-in-a-solution
If the VB Project does not allow linking with the SharesAssemblyInfo.cs then we could create another library with SharedAssemblyInfo.vb.
If duplication is not wished for then a library called SharedAssemblyInfo can be created in C#/VB with public properties. These public properties could then we referred to in both C# and VB projects in their respective AssemblyInfo.cs files

How to create a dll that includes all the others?

At the moment of creating a project of type "Library of Classes, usually one can generate a dll when compiling, but how could I generate a dll without losing others that I already have included?
I explain with an example: It turns out that Nuget downloaded an S22.Imap dll with the one I worked with, later I generated the dll in the traditional way that I explained in the beginning, but when I wanted to work with dll in another computer, I got errors that were not I found functions that contained the S22.IMAP dll. So to solve this problem, I had to copy the dll of my project, S22.IMAP in an additional way in a specific path of the other computer.
My question is:
How could you generate a dll that includes the ones included in the project you were working with?
All the referred 3rd party dlls (S22.Imap.dll in your example) will be copied to the output folder together with your own dll file (let's say a.dll) when you build your project. That means you should always copy them together (S22 + a.dll) to the place you want to refer them, on another computer/folder/place.
If you really want to make them only one file (although it is not recommended), you can set the S22 one as some "nested resource". Then you will get only one a.dll file and the S22 one is inside the a.dll. See below page for some reference:
Embedding one dll inside another as an embedded resource and then calling it from my code
AND, ILMerge is some tool that can help you do so.
In general, you don't. A DLL is a dynamic linked library, and you would normally only combine static libraries during a build. Here is an answer on the difference between static and dynamic linking.
Typically you would include all the DLLs you need in the installer package. If you use Visual Studio to create the installer, it can detect the dependencies for you. When you run the installer, all of the necessary DLLs are deployed. Nearly all commercial .NET software follows this pattern.
It is possible to merge an assembly into another assembly using a tool called ILMerge. This would be a very unusual thing to do, and could cause issues with intellectual property and code signing, so it is not recommended.

C# How to build dll from net modules

In my project, I want to reuse some code for multiple binaries (.exe).
So, I decided to build a .dll from some sources and to include it in my .exe application thanks to csc.exe.
OK that works.
But now, I want to add a new level : I would like to build some net modules and then build my .dll which includes all net modules built before.
Is that possible ? How ?
I think you are going to end up with a multiple file assembly if you use the C# compiler to do that.
But link.exe (which comes with Visual C++) should be able to produce a single file assembly from a bunch of .netmodule files.
This is possible. If you compile your projects all as .netmodules you can then link them into 1 single assembly. Instructions can be found here.
/*for one file */
csc /target:module misource1.cs
/*for multiple file */
csc /target:module misource1.cs misource2.cs misource3.cs
Target is Module so keep it before the source file names

How do I compile C# code as a library instead of an executable?

I have a C# console application in Visual Studio 2010. It has a Main() method as well as a bunch of utility classes. I'd like those utility classes to be available to other solutions. From reading online it seems that I need to compile it as a Class Library (DLL). So here's what I did:
Went in Visual Studio to "Project > [ProjectName] Properties > Application" and changed "Output type" from "Console Application" to "Class Library"
Rebuilt; ProjectName.dll was created in bin/Debug.
Created a new Console Application
Solution Explorer > Add Reference > browse to ProjectName.DLL, select it.
However, neither IntelliSense nor the Object Browser could find the classes inside that DLL.
I tried recompiling several different Console Applications as Class Libraries and got the same result. I also noticed that it works if I initially create the solution as a Class Library, but not if I convert it to one later.
Any tips?
You do not need to build it as a dll. VS 2010 (and IIRC 2008) allow referencing exe assemblies. All you need is for they relevant types to be declared public - top-level classes defualt to internal if you don't add a specifier.
You can switch output type to Class library in project properties as well - then you will have an output as dll instead exe file
What I've always done (since this is what you do with C++ static libraries, which is what I normally use - though I think it has some advantages for C# too) is add the class library's project to the solution, then add a reference to it in the project (or projects) that uses it.
When you go to add a reference, the list of potential references includes items from the solution, so it should be fairly obvious what to do. You should then get intellisense for your library.
One advantage of doing things this way is that if you need to edit files in the library project, it's very straightforward because they are close to hand, and the project then gets rebuilt automatically when you compile the solution.
Make sure that the classes in your dll project are public.
At first, from the point of view of managed libraries it does not matter what kind of Output type is your managed library. I mean that you can successfully reference ConsoleApplication1.exe from ConsoleApplication2.exe project (so you have no reason to convert ConsoleApplication1.exe to ConsoleApplication1.dll).
At second, I've tried to reproduce your situation, but... without effect. My VS displays types/methods from ConsoleApplication1.dll. One reason I can suppose is that you have forgotten to set visibility modifier (public keyword) for your utility classes.

Categories

Resources