C# How to build dll from net modules - c#

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

Related

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

why would a .cs file not see updates to an included dll file

I have a dll containing classes to access data in SQL (a sort of ORM system) included in my .cs page with a using statement. For some reason the dll (with definition for a new field) isn't seen by the cs code, though I've uploaded the new dll in bin. It won't see my new field in the dll's helper classes (now compiled into the dll).
Is there a way to troubleshoot the dll, or the cs to tell why this won't see the class I updated and rebuilt? The class works fine locally and on another server, but on my prod server, it bombs.
This is using Sitefinity 3.7 with a Subsonic/Substage module if that sheds some light on it.
If you are using Visual Studio, verify 2 things, first:
try deleting csproj.user and .suo files (visual studio will recreate them)
The second thing is the version of the framework your project is running, and the version of the framework the dll was compiled in.
If your project is using .NET 4.0 but the DLL was built using 2.0 or similar you may not be able to use it, you can add it, but it wont be loaded.
This sounds so familiar... have you check to see if there is another dll on the path that gets resolved? Dynamic-Link Library Search Order
Make sure that your dll was not registered on the production service in the GAC.
How to extract an assembly from the GAC?
Perhaps you have a local copy of the DLL in your project and the DLL that gets updated is elsewhere.
I tend to think the dll you build is 32 bit (X86) dll. where as you are trying to consume it from project that targets "Any CPU".
Is your production server a 64 bit ?
If answer is yes, goto project properties => Build tab (of your cs code's project which is not understanding the dll) and set the Platform target as X86.
If the updated DLL has a different version number, you may need to update the Project Reference to it by deleting and re-adding a reference to the DLL in the bin folder.
If the project generating the DLL is present in the same solution, you may have an issue in creating a file reference (may not be updated) instead of a project reference (will be updated).
fuslog.exe is a great tool when troubleshooting assembly (dll) binding issues.
http://msdn.microsoft.com/en-us/library/e74a18c4.aspx
Another .net developer helped me figure this out. I had a rogue ToString() in there where there should have been a cast to string, allowing nulls. My dll was okay after all. Thanks everyone for your suggestions, I learned a lot.

C# Merging .dll files with ILMerge

I am facing a problem with ILMerge. I have 4 dll files required for my app but I merged them into 1 with ILMerge using the following syntax:
ilmerge /out:merged.dll lib1.dll lib2.dll lib3.dll lib4.dll
Then I added the file merged.dll as a reference in my solution and removed the other 4. But my application still fails to load when the other dll files are not in the directory of the application.
Did I miss any step? It doesn't make any sense to me why it wouldn't work..
Fail on my part. I was testing on an older build of my application, which explains why it didn't work. The above steps will work fine so I hope this may be useful to someone.

C#'s equivalent of jar files?

Java provides the jar file so that all the class files and jar files are merged into one file.
Does C# provide equivalent/similar functionality?
.NET compiles into dll or exe. You can use ILMerge to merge several dlls/exes into one.
Aren't .NET assemblies just for this?
Remember, you can include resources, etc in it.
Also, assemblies could be combined using ILMerge, and for more complex scenarios you probably should better use ClickOnce or MSI deployment.
For silverlight, there's XAP packages, but I assume you're talking about desktop .NET.
It's called an "assembly".
http://en.wikipedia.org/wiki/.NET_assembly
The jar equivalent in C# (basically in any .Net language) is dll (for class library) and exe (for executable one) or collectively assembly. But one assembly can not include another assembly in the form of dll or exe. But ILMerge do merges two assemblies but not include one in another like jar file.
But there is project published in codeproject (http://www.codeproject.com/KB/install/NARLoader.aspx) you might get interested in. It do stuff like jar files with the .net assemblies.
Not really, C# works with .dll and .lib. If you don't put everything in the same project (all source code), you won't be able to achieve what you probably want to do.
But with ILMerge, you can combine everything into 1 executable for easier distribution if you don't want to have a setup or a compressed file containing all the files needed..
yes C# provides dll
Dynamic-Link Libraries
No, an assembly AFAIK can not include referenced assemblies.

Categories

Resources