Inline IL in C#

One of the features I liked in C++ was the ability to include inline assembly. Granted I never found much practical use for it but it was a great way to learn assembly without having to write a whole application in it.

Well a very clever Canadian by the name of Roy has now made that possible in C# and VB.NET. Not only is it a great feature but the way he implemented it is very clever and can be applied to do your own IL manipulation of your binaries.

Basically Roy uses the existing Visual Studio tools ildasm and ilasm to decompile your binary into IL, insert your IL code blocks, and recompile the result.

You specify your IL by enclosing it inside an #if directive, like so:

public static int Add(int n, int n2) {
#if IL
    ldarg n
    ldarg n2
    return 0; // place holder so method compiles

Adding this feature to your project is as simple as adding a post-build step. You can find the whole article here:

Leave a Reply

Your email address will not be published. Required fields are marked *