CompileToMethod无法编译常量'<some value>',因为它是一个非平凡的值,例如活动对象

Osc*_*nek 2 c# linq expression cil expression-trees

我正在尝试使用名为EvaluateOnCondition的方法创建一个带有Reflection.Emit的类型.我使用Linq表达式生成方法的主体,我想用LambdaExpressionCompileToMethod方法将表达式的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) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\TypeBuilders\LanguageTypeBuilder.cs:line 343
   at Integra.Space.Language.Runtime.LanguageTypeBuilder.CreateTypeBuilder(List`1 listOfFields, Type parentType, Boolean overrideGetHashCodeMethod, Boolean overrideEquals, Type typeOtherSource, Boolean isSecondSource, LambdaExpression onCondition) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\TypeBuilders\LanguageTypeBuilder.cs:line 165
   at Integra.Space.Language.Runtime.LanguageTypeBuilder.CompileExtractedEventDataComparerTypeForJoin(Type parentType, Type typeOfTheOtherSource, Boolean isSecondSource, LambdaExpression onCondition) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\TypeBuilders\LanguageTypeBuilder.cs:line 92
   at Integra.Space.Language.Runtime.ObservableConstructor.CreateProjectionExpression(PlanNode plans) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 3033
   at Integra.Space.Language.Runtime.ObservableConstructor.CreateExpressionNode(PlanNode actualNode, Expression leftNode, Expression rightNode) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 358
   at Integra.Space.Language.Runtime.ObservableConstructor.GenerateExpressionTree(PlanNode plan) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 304
   at Integra.Space.Language.Runtime.ObservableConstructor.GenerateExpressionTree(PlanNode plan) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 313
   at Integra.Space.Language.Runtime.ObservableConstructor.GenerateExpressionTree(PlanNode plan) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 299
   at Integra.Space.Language.Runtime.ObservableConstructor.GenerateExpressionTree(PlanNode plan) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 299
   at Integra.Space.Language.Runtime.ObservableConstructor.GenerateExpressionTree(PlanNode plan) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 313
   at Integra.Space.Language.Runtime.ObservableConstructor.GenerateExpressionTree(PlanNode plan) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 313
   at Integra.Space.Language.Runtime.ObservableConstructor.CreateObservableJoin(PlanNode actualNode) in C:\Users\Oscar\Documents\GitHubVisualStudio\Integra.Space.Language\Integra.Space.Language\Runtime\ObservableConstructor.cs:line 936
Run Code Online (Sandbox Code Playgroud)

onCondition变量的Debug视图

.Block(System.Boolean $variable) {
    .Try {
        .Block() {
            .If (
                True
            ) {
                .Call System.Diagnostics.Debug.WriteLine("Start of the equal operation '==': ")
            } .Else {
                .Default(System.Void)
            };
            $variable = (System.Object).Block(System.Object $variable) {
                .If (.Constant<System.Reflection.RuntimePropertyInfo>(System.Object _adapter_Name) == null) { // 12 line
                    $variable = .Default(System.Object)
                } .Else {
                    .Try {
                        .Block() {
                            .If (
                                True
                            ) {
                                .Call System.Diagnostics.Debug.WriteLine("Start of the get property operation: _adapter_Name")
                            } .Else {
                                .Default(System.Void)
                            };
                            $variable = $NewScopeParameter_0._adapter_Name;
                            .If (
                                True
                            ) {
                                .Call System.Diagnostics.Debug.WriteLine("End of the get property operation: _adapter_Name")
                            } .Else {
                                .Default(System.Void)
                            };
                            .Default(System.Void)
                        }
                    } .Catch (System.Exception $var1) {
                        .Block() {
                            .Call System.Diagnostics.Debug.WriteLine("No fue posible obtener la propiedad _adapter_Name, error en la linea: 0 columna: 162 con t1.@event.Adapter.Name")
                            ;
                            .Throw .New Integra.Space.Language.Exceptions.RuntimeException(
                                "RuntimeException: Line: 0, Column: 162, Instruction: t1.@event.Adapter.Name, Error: RE5: Error with the get property operation",
                                $var1)
                        }
                    }
                };
                $variable
            } == (System.Object).Block(System.Object $variable) {
                .If (.Constant<System.Reflection.RuntimePropertyInfo>(System.Object _adapter_Name) == null) {
                    $variable = .Default(System.Object)
                } .Else {
                    .Try {
                        .Block() {
                            .If (
                                True
                            ) {
                                .Call System.Diagnostics.Debug.WriteLine("Start of the get property operation: _adapter_Name")
                            } .Else {
                                .Default(System.Void)
                            };
                            $variable = $NewScopeParameter_1._adapter_Name;
                            .If (
                                True
                            ) {
                                .Call System.Diagnostics.Debug.WriteLine("End of the get property operation: _adapter_Name")
                            } .Else {
                                .Default(System.Void)
                            };
                            .Default(System.Void)
                        }
                    } .Catch (System.Exception $var2) {
                        .Block() {
                            .Call System.Diagnostics.Debug.WriteLine("No fue posible obtener la propiedad _adapter_Name, error en la linea: 0 columna: 188 con t2.@event.Adapter.Name")
                            ;
                            .Throw .New Integra.Space.Language.Exceptions.RuntimeException(
                                "RuntimeException: Line: 0, Column: 188, Instruction: t2.@event.Adapter.Name, Error: RE5: Error with the get property operation",
                                $var2)
                        }
                    }
                };
                $variable
            };
            .If (
                True
            ) {
                .Call System.Diagnostics.Debug.WriteLine("End of the equal operation")
            } .Else {
                .Default(System.Void)
            };
            .Default(System.Void)
        }
    } .Catch (System.Exception $var3) {
        .Block() {
            .Call System.Diagnostics.Debug.WriteLine("Error con la expresion de igualdad en la linea: 0 columna: 167 con t1.@event.Adapter.Name == t2.@event.Adapter.Name")
            ;
            .Throw .New Integra.Space.Language.Exceptions.RuntimeException(
                "RuntimeException: Line: 0, Column: 167, Instruction: t1.@event.Adapter.Name == t2.@event.Adapter.Name, Error: RE43: Error with the equal operation '=='",
                $var3)
        }
    };
    $variable
}
Run Code Online (Sandbox Code Playgroud)

我怀疑这个核心错误被抛出了.

BoundConstants第133行

顺便说一句:C#中的非平凡价值是什么?我怎么能解决这个问题?

如果您需要更多信息,请告诉我

xan*_*tos 5

什么是C#中的非平凡价值?我怎么能解决这个问题?*

一般来说null,bool,char,sbyte,byte,short,ushort,int,uint,long,ulong,float,double,decimal,string,Type,MethodBase都OK.其他一切都不是.

你可以从VariableBinder.VisitConstant以下开始看到它:

if (ILGen.CanEmitConstant(node.Value, node.Type))
{
    return node;
}
this._constants.Peek().AddReference(node.Value, node.Type);
Run Code Online (Sandbox Code Playgroud)

走向AddReference糟糕,从长远来看会导致你看到的异常.

然后,您可以查看ILGen.CanEmitConstant并查看已完成的测试.

为什么会这样?因为您正在尝试在方法内编译一个对象.虽然这通常是可能的,如果你使用LambdaExpression.Compile(),因为对象并没有真正保存在方法内,而只是对生活在内存中的对象的引用,如果你使用CompileToMethod().NET必须能够保存副本方法内部的对象,因为CompileToMethod()(和MethodBuilder/ TypeBuilder/ AssemblyBuilder)能够生成新的dll.这些dll可以与其他机器共享/加载几天后(它们是"正常"dll),因此不保证它们仅在当前程序的生命周期内运行.为此,.NET需要序列化对象,然后在调用方法时反序列化它.但这不受支持.显然,对于某些原始类型不会发生这种情况(并且我会注意到并非所有的.NET原语类型都受支持.缺少的是IntPtrUIntPtr)以及一些特殊类型Type(在.NET中有特殊处理).