我正在尝试使用名为EvaluateOnCondition的方法创建一个带有Reflection.Emit的类型.我使用Linq表达式生成方法的主体,我想用LambdaExpression的CompileToMethod方法将表达式的IL注入到EvaluateOnCondition中,但是当我执行CompileToMethod方法时,我收到以下错误:
CompileToMethod无法编译常量' 某个值 ',因为它是一个非平凡的值,例如活动对象.而是创建一个可以构造此值的表达式树.
这就是我想要做的:
MethodBuilder evaluateOnCondition = tb.DefineMethod("EvaluateOnCondition", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new[] { typeof(object), typeof(object) });
onCondition.CompileToMethod(evaluateOnCondition); // throw the error
Run Code Online (Sandbox Code Playgroud)
onCondition变量是LambdaExpression,是使用Linq表达式创建的比较条件,表示以下伪代码:obj1.prop1 == obj2.prop1
这是我的StackTrace:
at System.Linq.Expressions.Compiler.BoundConstants.EmitCacheConstants(LambdaCompiler lc)
at System.Linq.Expressions.Compiler.LambdaCompiler..ctor(AnalyzedTree tree, LambdaExpression lambda, MethodBuilder method)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, MethodBuilder method, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.LambdaExpression.CompileToMethodInternal(MethodBuilder method, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.LambdaExpression.CompileToMethod(MethodBuilder method)
at Integra.Space.Language.Runtime.LanguageTypeBuilder.CreateEqualsMethod(TypeBuilder tb, Type parentType, Type typeOtherSource, Boolean isSecondSource, LambdaExpression onCondition) …Run Code Online (Sandbox Code Playgroud)