Bernie Cook's Blog

Azure, C#, .NET, Architecture & Related Tech News

Using dotPeek to Review IL Weaving

Leave a comment

dotPeekThere are two types of Aspect Oriented Programming (AOP) in .NET; Interceptors and IL weaving. This post focuses on the later and illustrates how you can use a .NET decompiler to review the code generated by AOP post compilation processing using the IL Weaving approach, albeit the decompiled code.

The most well known AOP framework for .NET is PostSharp, which is what I’ll be using in this post. And one of the more well known .NET decompilers is dotPeek, developed by JetBrains, the company best known in the .NET world for ReSharper. You do have to pay for PostSharp (although there is a trial period), however dotPeek is free and simple to download and execute.

So what I’ve done is provide (1) the source code for a simple .NET console application I’ve knocked together, displaying the resulting decompiled code in dotPeek, then (2) I decorate one of the application’s methods with an AOP aspect and display the resulting decompiled code so you can see what PostSharp has done.

I’m going to assume you’re familiar with basic AOP terminology and will skip over it, having said that this is a very simple AOP example (I pinched the aspect from a PostSharp starter tutorial) so it hopefully shouldn’t pose too confusing if you’re new to AOP. I’m also running dotPeek with the “Show compiler-generated code“, in case you were wondering … or not 🙂

Before IL Weaving

Here’s a sample class in .NET. As you can see it has no aspect applied so it’s decompiled code will look as you’d expected from the screenshot below.

Program.cs
class Program
{
    static void Main(string[] args)
    {
        const string quote = "Better three hours too soon than a minute too late.";

        Console.WriteLine(quote);

        var vowelessQuote = RemoveVowels(quote);

        Console.WriteLine(vowelessQuote);

        Console.ReadKey();
    }

    static string RemoveVowels(string value)
    {
        var vowels = new List {"a", "e", "i", "o", "u"};

        return new string(value.Where(v => !vowels.Contains(
                   v.ToString(CultureInfo.InvariantCulture))).ToArray());
    }
}
dotPeek’s Decompiled Code View

dotPeek Before

After IL Weaving

So below we have our Aspect (courtesy of PostSharp), our source code update where the aspect is decorating the RemoveVowels() method, and the resulting decompiled code in the screenshot.

ProfileAspect.cs
[Serializable]
class ProfilerAspect : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        args.MethodExecutionTag = Stopwatch.StartNew();
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        var stopwatch = (Stopwatch)args.MethodExecutionTag;
        stopwatch.Stop();

        Console.WriteLine("{0} tool {1}ms to execute",
             args.Method.Name,
             stopwatch.ElapsedMilliseconds);
    }
}
Program.cs (with Aspect)
[ProfilerAspect]
static string RemoveVowels(string value)
{
    var vowels = new List {"a", "e", "i", "o", "u"};

    return new string(value.Where(v => !vowels.Contains(
               v.ToString(CultureInfo.InvariantCulture))).ToArray());
}
dotPeek’s Decompiled Code View

dotPeek After

Wrap Up

So this isn’t rocket science by any stretch, however it does show you that looking under the hood at the modified assembly can be quite informative and potentially helpful if you’re trying to debug a rather complex aspect. It also provides a better understanding of what happens as a result of IL weaving and the changes that AOP post processing makes to your compiled code.

Enjoy.

Advertisements

Author: Bernie

I currently live and work in the UK, just outside of London. I've been working in IT for 15+ years and in that time have solved many technical problems via blogs, forums, tutorials etc. and feel like I should be giving something back. This blog post is my way of contributing and I hope it proves just as useful to others as their contributions have to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s