Click here to Skip to main content
Click here to Skip to main content

Editing an assembly's manifest and more...

, 18 Jun 2005
Rate this:
Please Sign up or sign in to vote.
An article on editing an assembly's manifest and some other IL attributes.

Introduction

This article will provide a step-by-step guide to editing an assembly's manifest and a brief example of how easy it is to modify an assembly.

The Problem

Working in a larger development team means that you may not always have access to source code for a particular assembly, or if you do, you may not be able to compile it for whatever reason (e.g. you are out at a client site).

I recently had two problems that I solved by disassembling an assembly, modifying the manifest and IL and then reassembling it.

The attached code (above) has two projects. The first project is a WinForms project that uses a class library. The class library has the following lines of code:

using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyVersion("1.0.1.0")]

public class TestClass
{
    public static decimal SquareNumber(decimal baseNumber)
    {
        return CalculatePower(baseNumber, 2);
    }

    private static decimal CalculatePower(decimal baseNumber, int exponent)
    {
        decimal returnValue = 1;

        for (int i = 0; i < exponent; i++)
            returnValue *= baseNumber;

        return returnValue;
    }
}

Let's suppose for an instant that our class library is used in a number of places and we are recompiling it to fix a problem in a previous version. We need to distribute this to our client but we have compiled it for the wrong version and cannot use the new assembly because of version dependency problems.

Some new functionality in our WinForms application needs access to the CalculatePower method, which unfortunately has a private accessor. This means we will need to change its accessor to public instead of private (see below).

MessageBox.Show (TestClass.CalculatePower(decimal.Parse(txtBase.Text), 
                 int.Parse(txtExponent.Text)).ToString(), 
                 "Result", 
                 MessageBoxButtons.OK, 
                 MessageBoxIcon.Information);

The Solution

Thankfully, Microsoft has included all the tools you need to do this with Visual Studio .NET.

  1. Start the Visual Studio .NET 2003 Command Prompt.

    Visual Studio .NET Command Prompt

  2. At the command prompt, type ildasm <enter>.
  3. Open the assembly that you need to work on. In our case, this will be SatelliteAssembly.dll.
  4. After opening the file, select File->Dump. Ensure all checkboxes are selected and then click OK.
  5. You will be prompted to save it as an IL file. I recommend creating a new directory to save in. Save as SatelliteAssembly.IL (then close ILDASM).
  6. Open SatelliteAssembly.IL inside your favourite editing tool (I use Visual Studio .NET 2003).
  7. Find the IL code that looks like this:
    .assembly /*20000001*/ SatelliteAssembly
    {
      // --- The following custom attribute is added
      //                   automatically, do not uncomment -------
      //  .custom /*0C000001:0A000002*/ instance void [mscorlib/* 23000001 */]
      //         System.Diagnostics.DebuggableAttribute/* 01000004 */::.ctor(bool,
      //         bool) /* 0A000002 */ = ( 01 00 01 01 00 00 ) 
      .hash algorithm 0x00008004 
      .ver 1:0:1:0
    }
  8. Change the version 1Blush | :O :1Blush | :O (1.0.1.0) to 2Blush | :O :1Blush | :O (2.0.1.0).
  9. Find the IL code that looks like this:
      .method /*06000002*/ private hidebysig static 
              valuetype [mscorlib/* 23000001 */]System.Decimal/* 01000002 */ 
              CalculatePower(valuetype [mscorlib/* 23000001 */]
                System.Decimal/* 01000002 */ baseNumber,
                int32 exponent)}
  10. Change the private accessor to public.
  11. Ensure that the command prompt working directory is the temporary directory that you used to save the IL file.
  12. At the command prompt, type ilasm /DLL SatelliteAssembly.IL <enter>.

You will now be left with an assembly that has the new version number and the public method accessor. This can come in handy when you don't have explicit access to the source code or the source code is unavailable.

Points of Interest

Obviously, the power that is provided by the disassembly / assembly is quite phenomenal and there are many other tweaks and changes that can be provided by this facility.

I have not tried this method on obfuscated code (for development, our code is not obfuscated). If you have tried this on obfuscated code, let me know how it worked out.

ILASM has a number of switches that you may like to investigate (e.g. /DEBUG provides the .PDB file for debug information).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Simon McEnlly
Web Developer
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionSigned Assemblies PinmemberAmirAlilou16-Jan-14 3:01 
Questionupdating manifest of an assembly Pinmembershilpapc23-Nov-11 1:23 
GeneralNice!!! PinmemberPalanisamy Veerasingam19-Jul-07 4:17 
GeneralToo cool PinmemberDhananjayShenolikar12-Jul-07 2:18 
GeneralToo good Pinmemberjigjosh5-Sep-06 2:30 
Thanks for making things Clear.
GeneralThanks Pinmemberoykica19-Jun-05 15:34 
Generalit's a good tutorial Pinmembersilas_yao18-Jun-05 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Jun 2005
Article Copyright 2005 by Simon McEnlly
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid