Is there a good wrapper around ILGenerator? [closed]
Asked Answered
G

3

8

I'm using System.Reflection.Emit for a while now, and find it (who don't?) as painful as bug prone.

Do you know if there is a good wrapper around the IL Generator, something that I can rely on to emit IL in a more safe and easier manner than with playing directly with SRE?

Edit:

I know that manipulating expression trees is definitively easier and safer than emitting IL directly, but they also have some constraints right now. I can't create code blocs, use loops, declare and work with several locals, etc. We need to wait until .NET 4 comes out :)

Moreover, I'm dealing with a code base which already relies on SRE.

Obviously, ILGenerator do everything I need. But I would appreciate more assistance when manipulating it. When I'm referring to a ILGenerator wrapper, which remains at a pretty low level, I think about something which could provide methods like:

// Performs a virtual or direct call on the method, depending if it is a 
// virtual or a static one.
Call(MethodInfo methodInfo)

// Pushes the default value of the type on the stack, then emit 
// the Ret opcode.
ReturnDefault(Type type)

// Test the object type to emit the corresponding push 
// opcode (Ldstr, Ldc_I*, Ldc_R*, etc.)
LoadConstant(object o)

It's really 3 naive examples, but it could be enough to demonstrate what I expect. We can see that as a set of extension methods, but it could be nice to have support for conditional statements and loops like in RunSharp. In fact, RunSharp is pretty close that what I want, but it abstracts the ILGenerator too much and doesn't expose all its functionality.

I can't remember where, but I already saw such an helper in an open source project.

God answered 13/10, 2008 at 8:26 Comment(6)
You haven't specified what it is that ILGenerator doesn't do, and what a 'good' wrapper would do.Tetrabasic
That is a valid point ; I updated the question to provide more detail.God
Actually, as Expressions are lambda expressions, you should be able to use looping, implemented as recursion + Y operator.Griffy
What do you call Y operator ?God
I think it's called Y combinator — google it.Tailgate
The answer to this question is resoundingly SIGIL! github.com/kevin-montrose/Sigil and a great sample on it gist.github.com/bretcope/a9dea0c5c67b0bc051c8Particularly
E
6

If you're using .NET 3.5, you may find using Expression Trees to be more reasonable. It entirely depends on what you're doing - and it can still be quite painful - but it's certainly another option to be aware of.

Extrauterine answered 13/10, 2008 at 8:32 Comment(2)
Unfortunately, I'm still with .NET 2.0. I could use Mono implementation of Expression class, but expression trees are not able to handle what I aim to do ( code blocs, several local allocations, etc.)God
Extension Trees are not useful when implementing complex types. Only when working with methods.Manslaughter
C
4

[updated]: I thought of the name ;-p RunSharp. I can't vouch for it, but it might be what you need.

However; what do you need to generate? CodeDom is one option. For creating methods, you might find that you can do a lot more than you expect with the Expression class in .NET 3.5 (after compiling it to a typed delegate via Expression.Lambda/Compile.

Catwalk answered 13/10, 2008 at 8:33 Comment(3)
I am aware of RunSharp, and I was looking for something like that which deals at a lower level. I prefer to stay away from CodeDom, even if it is a good solution in many cases. I'm stuck with DynamicMethods most of the time.God
OK; minor thought, but you might have included this (and your 2.0 requirement) in the original question... Re "lower level" - what sort of level (between RunSharp and ILGenerator) are you looking for?Catwalk
You are right, I added some more info in the original question. Thanks for your help!God
T
2

Try using Mono.Cecil

Cecil is a library written by Jb Evain to generate and inspect programs and libraries in the ECMA CIL format. It has full support for generics, and support some debugging symbol format.

Theogony answered 13/10, 2008 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.