Click here to Skip to main content
15,501,347 members
Articles / Programming Languages / MSIL
Posted 19 Feb 2007


53 bookmarked

Mono.Cecil, The Most Powerful Tool You've Ever Heard Of

Rate me:
Please Sign up or sign in to vote.
4.29/5 (13 votes)
19 Feb 2007CPOL2 min read
Experimentation with Mono.Cecil, the IL Manipulation library


At my current employer, the need for being able to "Sign" some additional information to an assembly came about. As an option, I could have always just created a CustomAttribute for the assembly, and recompiled each and every time I needed to, or actually injected some data after the fact.

I happened to come across a project that had very little documentation called Cecil (Mono.Cecil). Cecil is an IL modification library that is very powerful. Using Cecil, one can open up any existing .NET assembly, modify IL, bind events, remove functionality, add new functionality, etc., and then proceed to write the new assembly out to disk, or a byte[].

Basically when it comes down to it, in an effort to find a solution to a simple problem, I came across a tool that was way more powerful than I had ever heard of.

I intend to start out with some very basic examples and eventually dive into some more complex examples, so stay tuned!

The Custom "Signing" Example

For this example, I'll be signing a simple console application with some CustomAttributes, AFTER compile time.

I'm no expert with the post compilation tasks, but basically you'll want to compile dummyapp first, and copy the resulting EXE into the bin path of the AppSigner project (Program.cs).

using System;
using System.Collections.Generic;
using System.Text;
using Mono.Cecil;

namespace AppSigner
    class Program
        static void Main(string[] args)
            Console.WriteLine("Signing DummyApp.exe");
            // create an Assembly definition from target assembly... 
            // in this case I'll pass in full path to DummyApp.exe 
            AssemblyDefinition sourceAssembly 
                = AssemblyFactory.GetAssembly("DummyApp.exe");
            //Next, we'll need to "Import" a reference of the new 
            //CustomAttribute (which happens to exist in my signing 
            //app for the moment.)
            //typeof(string) denotes what the constructor parameter 
            //type is. If your attribute was of Guid you could use 
            //typeof(Guid) etc.
            CustomAttribute ca =
                new CustomAttribute(sourceAssembly.MainModule.Import(
                new Type[] { typeof(string) })));
            //Assign the parameters value of specified type
            ca.ConstructorParameters.Add("This is some extended information!");
            //go ahead and now add this customattribute instance 
            // back to the target assembly's CustomAttributes collection.
            //finally go ahead and persist back to disk. In this case 
            //for clarity, I went ahead and just wrote a new file out.
            AssemblyFactory.SaveAssembly(sourceAssembly, "newassembly.exe");
            Console.WriteLine("Signing Complete!");
            //now just to verify, we'll open the new assembly back up, 
            //and loop through custom attributes...
            AssemblyDefinition targetAssembly
                = AssemblyFactory.GetAssembly("newassembly.exe");
            foreach (CustomAttribute eca in targetAssembly.CustomAttributes)
                if (eca.Constructor.DeclaringType.Name == 
                    "newassembly.exe's ApplicationExtendedInformation attribute:");

You also need to define the AssemblyExtendedInfo class which will contain your assemblies extra information (AssemblyExtendedInfo.cs).

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

namespace AppSigner
    //mark this attribute as applicable to Assemblies
    public sealed class AssemblyExtendedInfo : Attribute
        private string extendedInfo = string.Empty;

        public string ExtenededInfo
                return extendedInfo;

        //this will be the constructor we'll be referring to later
        public AssemblyExtendedInfo(string extendedInfo)
            this.extendedInfo = extendedInfo;

        public override string ToString()
            return extendedInfo;

Points of Interest

Now many of you will wonder what the point is to this exercise considering that the new Attribute won't be visible within the assembly information in Windows Explorer etc. Well, you can easily use Simple Reflection to determine this value, or go ahead and reopen with the Mono.Cecil library. I hoped that this would give you a bit of insight into how powerful this library is, and what kind of modification you can make to an existing assembly without having to decompile or reflect out a bunch of source.

What's To Come?

I intend to follow up with at least two further articles explaining the capabilities of Mono.Cecil as well as further possible uses, etc. In the articles to come, I plan on going into rewriting functionality, as well as injecting tracing information into existing assemblies. I am somewhat new to Mono.Cecil so bear with me, I'll try to answer any questions I can!

Article History

  • 19th February, 2007 - Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
I've been involved with software development since teaching Basic to teachers in 6th grade. Since then I've been involved with every aspect of computers.

Lately, I've involved myself very much in building Entperise Java and .Net applications.

Comments and Discussions

QuestionGreat! Pin
alejandro29A12-Jul-13 6:02
Memberalejandro29A12-Jul-13 6:02 
thanks for this great article! Smile | :)
Dios existe pero duerme...
Sus pesadillas son nuestra existencia.
(Ernesto Sabato)

GeneralMy vote of 1 Pin
Salvodif17-Jan-13 1:53
MemberSalvodif17-Jan-13 1:53 
GeneralNew GUID Pin
sabrown10026-Nov-07 13:19
Membersabrown10026-Nov-07 13:19 
GeneralDebug mode... Pin
pablodg1-Nov-07 11:39
Memberpablodg1-Nov-07 11:39 
GeneralNice library ! Pin
Juergen Posny20-Feb-07 0:19
MemberJuergen Posny20-Feb-07 0:19 
GeneralRe: Nice library ! Pin
ronnyek20-Feb-07 4:30
Memberronnyek20-Feb-07 4:30 
GeneralRe: Nice library ! Pin
JeremyWilkins28-Nov-13 8:44
MemberJeremyWilkins28-Nov-13 8:44 
GeneralAbstract IL Pin
Insincere Dave19-Feb-07 11:10
MemberInsincere Dave19-Feb-07 11:10 
GeneralUnsealing sealed classes Pin
Nish Nishant19-Feb-07 9:22
sitebuilderNish Nishant19-Feb-07 9:22 
GeneralRe: Unsealing sealed classes Pin
Ramon Smits19-Feb-07 11:20
MemberRamon Smits19-Feb-07 11:20 
GeneralRe: Unsealing sealed classes Pin
Philip Laureano20-Sep-07 17:48
MemberPhilip Laureano20-Sep-07 17:48 
GeneralGreat article! Pin
Alexey A. Popov19-Feb-07 8:03
MemberAlexey A. Popov19-Feb-07 8:03 
GeneralRe: Great article! Pin
ronnyek19-Feb-07 8:58
Memberronnyek19-Feb-07 8:58 
GeneralSigning for preventing unauthorized copiesُ Pin
Mehran Taheri Mood19-Feb-07 7:03
MemberMehran Taheri Mood19-Feb-07 7:03 
GeneralRe: Signing for preventing unauthorized copiesُ Pin
ronnyek19-Feb-07 7:11
Memberronnyek19-Feb-07 7:11 
GeneralRe: Signing for preventing unauthorized copiesُ Pin
Christian Klauser19-Feb-07 7:34
MemberChristian Klauser19-Feb-07 7:34 
GeneralRe: Signing for preventing unauthorized copiesُ Pin
ronnyek19-Feb-07 7:40
Memberronnyek19-Feb-07 7:40 
GeneralRe: Signing for preventing unauthorized copiesُ Pin
_henke_26-Feb-07 23:22
Member_henke_26-Feb-07 23:22 
GeneralRe: Signing for preventing unauthorized copiesُ Pin
ronnyek27-Feb-07 4:29
Memberronnyek27-Feb-07 4:29 
GeneralRe: Signing for preventing unauthorized copiesُ Pin
sabrown10027-Oct-07 10:37
Membersabrown10027-Oct-07 10:37 

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

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