This project is read-only.

Emitting my own IL?

Apr 24, 2010 at 9:25 AM
Edited Apr 24, 2010 at 9:25 AM

Is it in any way possible to emit a few IL instructions of my own inside my expression trees in any way? Basically I need to emit IL that allows me to change the type of a reference from object to something else without the dynamic runtime check, would look something like this in IL:

.method public static !!b retype<a, b>(!!a x) cil managed
    .maxstack 3
    L_0000: nop 
    L_0001: ldarg.0 
    L_0002: ret 

This is a static method taking an argument of type a and returning it as type b, I need to emit the same type of IL instruction to do type casts without checks into the DLR, if it's possible already - how? (I have tried to find it) If not, can I plug something in to the DLR to do this? Or do I need to modify the DLR and use my own modified version of it? 

Apr 24, 2010 at 3:54 PM

The only way to put IL into an expression tree is to have a MethodCallExpression into a MethodInfo body. In this case, you could build a DynamicMethod that contains the IL you want.