How to insert CIL code to C#
Asked Answered
A

5

27

Is it possible to insert IL code to C# method?

Ambulant answered 25/7, 2010 at 10:57 Comment(0)
P
14

I just posted a utility which allows entire C# function bodies to be automatically replaced with inline IL, using a custom attribute. Like similar utilities, this works via the ILDASM/ILASM round-trip which can be set up as a post-build step. The tool also adjusts the PDB in order to preserve single-stepping and setting breakpoints on individual IL instructions in the debugger. It's different from some of the other round-trip IL inliners in that it (only) substitutes for entire function bodies, like this:

    class MyClass
    {
        [ILFunc(@"
    .locals init ([0] int32 i_arg)
        ldc.i4.3
        ret
    ")]
        int MyFunc(int i_arg)
        {
            return 3;
        }
    };

For highly performance-critical methods, I tried using DynamicMethod to improve upon compiler-generated IL, but found that the benefit is lost due to the delegate-calling overhead. Inline IL gives the the beneft of hand-tuned IL without that hit, assuming there are no runtime customizations that you would truly need DynamicMethod for.

The complete source code is located at http://www.glennslayden.com/code/c-sharp/inline-il

Palisade answered 10/5, 2011 at 9:32 Comment(4)
Didn't find any ILFunc attribute in your lib.Jorgensen
@Jorgensen I don't think I was providing a lib. It's source code, and there are two files on the web page. The second one (scroll down beyond the additional text) contains the definition for the ILFunc Attribute itself.Palisade
Is there a way to write more than just message bodies? So I can use features the cil supports but C# doesntCyndy
@Cyndy This method supports any valid CIL code accepted by ILASM.Palisade
M
4

DynamicMethod is the lightweight way to accomplish this at runtime.

The Microsoft C# compiler doesn't support injection of IL at compile-time, but a code-weaving tool could do so as a post-compile step.

Marva answered 25/7, 2010 at 18:37 Comment(0)
H
4

I'll add my own tool to the list of solutions already provided here: InlineIL.Fody.

This uses the Fody assembly weaving tool to modify the assembly at build time. Which means all you have to do is install a NuGet package, add a config file to your project and you're done.

You're then provided with a simple and type-safe API to emit IL instructions, which you can mix with C# code. I believe it's easier to write IL in this way than writing text, and it's also more convenient than ILGenerator since each opcode gets its own method with relevant overloads only.

Here's an example, with using static InlineIL.IL.Emit;:

public static void ZeroInit<T>(ref T value)
    where T : struct
{
    Ldarg(nameof(value));
    Ldc_I4_0();
    Sizeof(typeof(T));
    Unaligned(1);
    Initblk();
}

This shows how to access the initblk instruction which C# doesn't currently expose.

Hanni answered 9/9, 2018 at 20:55 Comment(1)
Woww, I didn't know such great extension! It works great! Seems like inline IL to me :)Marchese
I
3

If inline IL (in the same spirit of inline assembly supported by C and C++ compilers) is what you're looking for, this can be achieved using post-compilation round-trip compiling.

Mike Stall has once written a tool for that, as far as I know it's fairly mature:

Other than that, you could use F# which supports Inline IL.

Interjoin answered 25/7, 2010 at 11:2 Comment(1)
Do you know where to find Mike Stall tool? The link is broken. Thanks!Marchese
D
3

The tool you need is called Cecil and is a part of the Mono project.

You can get more information about it here: http://www.mono-project.com/Cecil

Quoted from the website above:

Cecil is a library written by Jb Evain (http://evain.net/blog/) to generate and inspect programs and libraries in the ECMA CIL format. It has full support for generics, and support some debugging symbol format.

In simple English, with Cecil, you can load existing managed assemblies, browse all the contained types, modify them on the fly and save back to the disk the modified assembly.

Deluge answered 25/7, 2010 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.