- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用名为 EvaluateOnCondition 的方法创建带有 Reflection.Emit 的类型。我使用 Linq Expressions 生成方法的主体,并且我想使用 CompileToMethod 将表达式的 IL 注入(inject)到 EvaluateOnCondition 中LambdaExpression 的 Strong> 方法,但是当我执行 CompileToMethod 方法时,出现以下错误:
CompileToMethod cannot compile constant 'some value' because it is a non-trivial value, such as a live object. Instead, create an expression tree that can construct this value.
这就是我正在尝试做的事情:
MethodBuilder evaluateOnCondition = tb.DefineMethod("EvaluateOnCondition", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new[] { typeof(object), typeof(object) });
onCondition.CompileToMethod(evaluateOnCondition); // throw the error
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
onCondition 变量的调试 View
.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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0571342b456073606b712b446164757160772b4b646860" rel="noreferrer noopener nofollow">[email protected]</a>")
;
.Throw .New Integra.Space.Language.Exceptions.RuntimeException(
"RuntimeException: Line: 0, Column: 162, Instruction: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c286d721c392a393228721d383d2c28392e72123d3139" rel="noreferrer noopener nofollow">[email protected]</a>, 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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9febadb1dffae9faf1ebb1defbfeefebfaedb1d1fef2fa" rel="noreferrer noopener nofollow">[email protected]</a>")
;
.Throw .New Integra.Space.Language.Exceptions.RuntimeException(
"RuntimeException: Line: 0, Column: 188, Instruction: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d793f234d687b686379234c696c7d79687f23436c6068" rel="noreferrer noopener nofollow">[email protected]</a>, 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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c682d325c796a797268325d787d6c68796e32527d7179" rel="noreferrer noopener nofollow">[email protected]</a> == <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f4b2aec0e5f6e5eef4aec1e4e1f0f4e5f2aecee1ede5" rel="noreferrer noopener nofollow">[email protected]</a>")
;
.Throw .New Integra.Space.Language.Exceptions.RuntimeException(
"RuntimeException: Line: 0, Column: 167, Instruction: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2155100f614457444f550f604540515544530f6f404c44" rel="noreferrer noopener nofollow">[email protected]</a> == <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6317514d230615060d174d220702131706114d2d020e06" rel="noreferrer noopener nofollow">[email protected]</a>, Error: RE43: Error with the equal operation '=='",
$var3)
}
};
$variable
}
我怀疑错误是在核心的这一行抛出的。
顺便说一句:C# 中的非平凡值(value)是什么?我该如何解决这个问题?
请告诉我您是否需要更多信息
最佳答案
What is non-trivial value in C#? How could I fix this?*
一般null
, bool
, char
, sbyte
, byte
, short
, ushort
, int
, uint
, long
, ulong
, float
, double
, decimal
, string
, Type
, MethodBase
还好。其他一切都不是。
你可以从 VariableBinder.VisitConstant
开始看到它:
if (ILGen.CanEmitConstant(node.Value, node.Type))
{
return node;
}
this._constants.Peek().AddReference(node.Value, node.Type);
前往AddReference
不好,从长远来看会导致您看到的异常。
然后你可以看 ILGen.CanEmitConstant
并查看已完成的测试。
为什么会发生这种情况?因为您正在尝试在方法内部编译对象。如果您使用LambdaExpression.Compile()
,这通常是可能的,因为该对象并未真正保存在方法内,而只是对内存中对象的引用(如果您使用 CompileToMethod()
) .NET 必须能够在方法内保存对象的副本,因为 CompileToMethod() (和 MethodBuilder
/TypeBuilder
/AssemblyBuilder
)能够生成新的 dll。这些 dll 可以与其他计算机共享/几天后加载(它们是“正常”dll),因此不能保证它们仅在当前程序的生命周期内运行。为此,.NET 需要序列化对象,然后在调用方法时反序列化它。但这不受支持。显然,某些基元类型不会发生这种情况(我会注意到,并非所有 .NET 基元类型都受支持。缺少 IntPtr
和 UIntPtr
)以及某些特殊类型,如 Type
。 (在 .NET 中有特殊处理)。
关于c# - CompileToMethod 无法编译常量 '<some value>',因为它是一个不平凡的值,例如事件对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35903724/
我试图为此搜索答案,但我发现很难找到这种“确切”的例子。我对指向指针的指针知之甚少,我觉得除了指向某物的指针之外,还有更多东西隐藏在它的表层之下。 那么你们会如何翻译呢? void free(sham
我有一个类的属性,比如const CFoo &bar() const,这是什么意思? 最佳答案 bar 方法返回对 const CFoo 的引用(即 bar 之前的 const CFoo & 部分),
例如是 int const x = 3; 有效代码? 如果是的话,意思是一样的 const int x = 3; ? 最佳答案 它们都是有效的代码并且它们都是等价的。对于指针类型,尽管它们都是有效代码
我知道 f(const T& obj) // (1) g(T const& obj) // (2) 是一样的。(我们不能改变f和g中obj的值)。 但是什么 h(T & const) // (3) 真
本节讲解的内容 include和include_once require和require_once 常量 引入文件和常量结合案列 变量操作函数 输出语句 前言 在上篇文章中,我们讨论了函数的应用,但是
我们知道我们可以保护变量的值,因此用户无法更改现有变量的值!这对对象来说没有什么问题吗?? 例如.. const x = 5; x = 10; alert(x) // will be returned
我正准备为 CUDA 设备编写直方图内核。它基于 NVIDIA's paper . 这个想法是每个线程计算某个部分(在我的例子中是体积)的部分直方图并将其写入共享内存块。然而,我遇到了一个奇怪的算法问
常量是固定值,程序执行期间不会改变。常量可以是任何基本数据类型,比如整数常量、浮点常量、字符常量或者字符串常量,还有枚举常量。 常量可以被当作常规的变量,只是它们的值在定义后不能被修改。 整数常
在这种情况下,如何识别是否有变量或字面量传递给函数 f()? 如何实现passed_as_constant()检查(见下面代码)? sub f { my $refStr=\$_[0]; ret
我目前想知道如何在 python 中列出 win32com 中的常量, 例如使用 excel win32com.client.Dispatch('Excel.Application') 有没有办法使用
这个问题在这里已经有了答案: PHP | define() vs. const (9 个回答) 关闭8年前。 在 PHP 中遇到常量问题想知道是否有人可以解释: 这行得通 const _ROOT =
我正在学习 Rust,到目前为止,似乎有 3 种声明变量的方法: const A: u8 = 42; static A: u8 = 42; let A: u8 = 42; 我知道你不能有一个可变的 c
我正在使用函数模板 void _createAttr(T)(args..., in T[]) 并使用 测试 T 的类型函数中的 static if(is(T == char)) 。当我打电话时, _c
这可能是一个天真的问题,我怀疑答案是"is",但我没有运气在这里和其他地方搜索“erlang编译器优化常量”等术语。无论如何,erlang 编译器是否可以(将)在编译时创建一个常量或文字的数据结构,并
我刚遇到这段 Java 脚本代码: const { myKey, uname, issorted, title, hClick, } = this.props; 请告诉我这是什么意
我正在努力实现以下目标: 我有一个父类,有一些逻辑。在子类中,我“重新定义”常量/属性。有没有办法让子属性可以通过父类中定义的方法访问?或者更具体地说 - 有什么方法可以强制“out”方法在下面的示例
如果这是个愚蠢的问题,请原谅。 我有一个带有内部类接口(interface)的“fragment ”外部类。该接口(interface)仅由另一个 Activity 类使用“implements Ou
我是 python 新手,尝试使用默认值并为类实例自定义它们。 因此,在这个示例中,我定义了一个 DEFAULT_STRING 和一个 DEFAULT_SETTINGS 变量,可以使用 customi
在 integer.xml 中,其形式为 0x001 0x002 是代码和 xml 文件都需要的存储常量。 C# 识别 Droid.Resource.Integer.foo,但它有一些大的
是否有跨平台(即跨 Linux、BSD 和 OS X,最好是所有 POSIX)我可以纯粹基于字符串 以编程方式访问诸如 O_RDWR 之类的常量>“O_RDWR”?我正在编写一些(非 C)代码,这些代
我是一名优秀的程序员,十分优秀!