Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Editing an assembly's manifest and more...

0.00/5 (No votes)
18 Jun 2005 1  
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 1:0:1:0 (1.0.1.0) to 2:0:1:0 (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