LambdaExpression CompileToMethod
Asked Answered
P

1

3

I Have a few lines of code

public void CreateMethod<TContract>(Expression<Action<TContract>> method)
{
   var innerMethod = Builder.DefineMethod("SomeName",MethodAttributes.Private);
   method.CompileToMethod(innerMethod);
   //more code
}

However the second line fails. I've tried with different versions of DefineMethod with little luck. Any suggestions?

Piny answered 21/10, 2010 at 22:5 Comment(0)
R
9

Unfortunately, CompileToMethod requires a static method as its argument (see here). Therefore, you need to add MethodAttributes.Static to innerMethod's definition.

Reptilian answered 22/10, 2010 at 0:23 Comment(2)
As further comments it's worth noting that you cannot pass an argument of the type the the method is compiled intoPiny
Here is a work-around based on interfaces: justinmchase.com/2009/01/08/…Shea

© 2022 - 2024 — McMap. All rights reserved.