Tuesday, March 06, 2007

* Tools : ILMerge

Few important points to be noted before we get to the usage of this utility.

  • Its a utility which merges multiple .NET assemblies into a single assembly.
  • It will not work if assembly contains unmanaged code.
  • Its a console application but all its functionality is available programmatically also.
  • The type of first assembly in the list of input assemblies determines whether target assembly is exe or dll.
  • It supports strong named assemblies also.
  • Download : It can be downloaded from here.

To understand its usage lets create two libraries lib1 and lib2 with following source code :

Let Lib1 contain the following code :

using System;
using System.Collections.Generic;
using System.Text;

namespace Lib1
{
public class Class1
{
public void GetHello()
{
System.Console.WriteLine("Hello from Lib1");
}
}
}






Let Lib2 contain the following source code :

using System;
using System.Collections.Generic;
using System.Text;

namespace Lib2
{
public class Class1
{
public void GetHello()
{
System.Console.WriteLine("Hello from Lib2");
}
}
}






Build both the assemblies and run ILMerge tool to generate a new assembly called LibUnited.dll



Use the following command :



ilmerge   /out:LibUnited.dll     lib1.dll       lib2.dll



Investigate the new dll using ildasm and you will the contents of both the input dll into it. To confirm the merge lets build a console app which uses this new dll.



Create a console app with following code & add a reference to LibUnited and not Lib1 or Lib2.

using System;
using System.Collections.Generic;
using System.Text;
using Lib1;
using Lib2;

namespace UseLib
{
class Program
{
static void Main(string[] args)
{
Lib1.Class1 lc1 = new Lib1.Class1();
Lib2.Class1 lc2 = new Lib2.Class1();
lc1.GetHello();
lc2.GetHello();
}
}
}






The output you should get is :



Hello from Lib1
Hello from Lib2



The main advantage of this tool comes in the scenario where large number of application components are getting developed/tested by independent teams and during integration of those components instead of having so many assemblies, they can be merged into a single assembly.



~tata & take care~



Other Posts



1 comment:

  1. Free UI Wrapper for ILMerge
    http://www.genetibase.com/cforce/nugenunify.php

    ReplyDelete