C# LambdaExpression编译方法

C# LambdaExpression编译方法,c#,.net,lambda,reflection.emit,C#,.net,Lambda,Reflection.emit,我有几行代码 public void CreateMethod<TContract>(Expression<Action<TContract>> method) { var innerMethod = Builder.DefineMethod("SomeName",MethodAttributes.Private); method.CompileToMethod(innerMethod); //more code } public void

我有几行代码

public void CreateMethod<TContract>(Expression<Action<TContract>> method)
{
   var innerMethod = Builder.DefineMethod("SomeName",MethodAttributes.Private);
   method.CompileToMethod(innerMethod);
   //more code
}
public void CreateMethod(表达式方法)
{
var innerMethod=Builder.DefineMethod(“SomeName”,MethodAttributes.Private);
方法:CompileToMethod(innerMethod);
//更多代码
}
然而,第二行失败了。我尝试过不同版本的DefineMethod,但运气不佳。
有什么建议吗?

不幸的是,
CompileToMethod
需要一个静态方法作为其参数(请参阅)。因此,您需要将
MethodAttributes.Static
添加到
innerMethod
的定义中。

作为进一步的注释,值得注意的是,您不能传递方法编译到的类型的参数。这是一个基于接口的解决方法: