This project is read-only.

Expression.Call( Lambda.compile() ) -- in essence

Apr 22, 2010 at 9:59 PM

I'm struggling with getting lambda.compile to work on an expression tree that contains a call to a delegate from an earlier lambda.compile.


I've compiled an expression tree using myDelegate= lambda.compile().

The resulting delegate works fine when I call myDelegate.invoke().

Now, I want to compile another expression tree that contains a call to this delegate.

Here's what I tried:

      MethodInfo myMethodInfo = myDelegate.Method;

       Expression CallMyDelegate = Expression.Call( myMethodInfo, expressionArgs );

       body = ... some block containing CallMyDelegate and other junk...

       LambdaExpression Lambda = Expression.Lambda(body);

That runs without throwing anything, but...

       Delegate del = Lambda.Compile();

chokes, saying:

     MethodInfo must be a RuntimeMethodInfo.
     Parameter name: meth


        internal void Emit(OpCode opcode, MethodInfo meth) {
            _ilg.Emit(opcode, meth); <-- right here BOOM
            _offset += 4;

from the OffsetTrackingILGenerator class about 30 frames deep in the compiler.

The debugger tells me that parameter meth is myMethodInfo from above.

Its type is:

System.Reflection.MethodInfo {System.Reflection.Emit.DynamicMethod.RTDynamicMethod}

If I replace CallMyDelegate with a call to some other method such as Console.Writeline, everything works fine.

I'm sorry if anyone feels this is off topic, but I'm doing this on the way into dynamic. Any suggestions as to a better place to post would be welcome.


So, Does anyone know how I can get a RuntimeMethodInfo, or compile a call to a RTDynamicMethod? or what?



Apr 22, 2010 at 11:00 PM

Use Expression.Invoke to create an expression that invokes a delegate.

Apr 23, 2010 at 12:50 AM

Thank You!

That worked like a charm.

Can you also tell where I might read so I don't have to ask you dumb questions?

Thanks again,




Apr 23, 2010 at 12:58 AM

DLR documentation:

Expression Trees specifically: