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

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

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.

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.

Missing DLLs from a class library that uses separate DLLs

In C# .NET 4.0 I am creating a class library / DLL that I plan to reuse in many other projects. This class library will use several DLLs itself. For example, the class library I am creating may reference several DLLs like:
ServerConnectorLibrary references:
Lib1.dll
Lib2.dll
Lib3.dll
When I build this, I get ServerConnectorLibrary.dll as the output in the bin\Debug folder.
What I want now is to use this ServerConnectorLibrary.dll in other projects. However, when I add the DLL to my new projects and run I get the following error:
FileNotFoundException: Could not load file or assembly "Lib1.dll".
If I add Lib1.dll to the bin\Debug folder of my new project, the problem is not solved.
You may be able to combine all of your dependencies into a single assembly. Here is an old example on CodeProject.
http://www.codeproject.com/Articles/9364/Merging-NET-assemblies-using-ILMerge
There is also a StackOverflow thread here...
How do I merge multiple .net assemblies into a single assembly?
Otherwise, make sure the properties for each dependency is set to copy local and then include the dependencies along with your library. These are DLLs, so they are dynamically linked at run-time, unlike older C++ and C libraries that might be statically linked.

build a c# project into two identical dlls with different assembly names and guids

I am wondering if it is possible to build a project into two identical dlls that have different assembly names and guids.
similar to the post build macros that allow one to copy and rename the dll - I imagine something like a pre-build command to build dll #1 with assembly name 1 and guid 1 and then build the same project again with assembly name 2 and guid 2.
I have searched the net but everything I could find pointed to the usual post build macros for renaming the dll file.
Many thanks in advance.
I haven't done that from a single project, but you can create a second (or third or fourth) project with a different assembly name/GUID, link files from the source project, and you can compile both simultaneously from the same solution.
You can use Project Linker to help keep the projects synchronized.

Creating DLL files in 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

Categories

Resources