![]() |
Languages »
MSIL »
General
Intermediate
Editing an assembly's manifest and more...By Simon McEnllyAn article on editing an assembly's manifest and some other IL attributes. |
C#, MSIL, Windows, .NET1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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);
Thankfully, Microsoft has included all the tools you need to do this with Visual Studio .NET.

.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
}
.method /*06000002*/ private hidebysig static
valuetype [mscorlib/* 23000001 */]System.Decimal/* 01000002 */
CalculatePower(valuetype [mscorlib/* 23000001 */]
System.Decimal/* 01000002 */ baseNumber,
int32 exponent)}
private accessor to public.
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.
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).
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Jun 2005 Editor: Smitha Vijayan |
Copyright 2005 by Simon McEnlly Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |