Reflector生成Lambda表达式,如何取回实际的Linq查询?
我正在使用 Reflector 反编译一些二进制文件。我在使用 Lambda 表达式和 方法 时遇到问题编译。 lambda 表达式对我来说似乎非常复杂,我不知道如何将其转换回可编译的 LINQ 代码。这是代码:
public double? LoadSumRialAmount(long functionId, long? subFunctionId)
{
ParameterExpression expression;
if (!subFunctionId.HasValue)
{
return (from func in base.MetaData.SubFunction
where func.FunctionId == functionId
select func).Select<SubFunctionEntity, double?>(System.Linq.Expressions.Expression.Lambda<Func<SubFunctionEntity, double?>>(System.Linq.Expressions.Expression.Multiply(System.Linq.Expressions.Expression.Property(expression = System.Linq.Expressions.Expression.Parameter(typeof(SubFunctionEntity), "func"), (MethodInfo) methodof(SubFunctionEntity.get_Volume)), System.Linq.Expressions.Expression.Property(expression, (MethodInfo) methodof(SubFunctionEntity.get_RialAmount))), new ParameterExpression[] { expression })).Sum();
}
return (from func in base.MetaData.SubFunction
where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
select func).Select<SubFunctionEntity, double?>(System.Linq.Expressions.Expression.Lambda<Func<SubFunctionEntity, double?>>(System.Linq.Expressions.Expression.Multiply(System.Linq.Expressions.Expression.Property(expression = System.Linq.Expressions.Expression.Parameter(typeof(SubFunctionEntity), "func"), (MethodInfo) methodof(SubFunctionEntity.get_Volume)), System.Linq.Expressions.Expression.Property(expression, (MethodInfo) methodof(SubFunctionEntity.get_RialAmount))), new ParameterExpression[] { expression })).Sum();
}
ps 另一个错误是 SubFunctionEntity 类具有“Volume”属性,我不明白为什么此代码在其上调用某种静态属性,例如: SubFunctionEntity.get_Volume
I'm using Reflector to decompile some binaries. I'm having problem with Lambda Expressions and methodof which doesn't compile. lambda Expression seems super complicated to me and I have no idea how to convert it back to Compilable LINQ Code. here is the code:
public double? LoadSumRialAmount(long functionId, long? subFunctionId)
{
ParameterExpression expression;
if (!subFunctionId.HasValue)
{
return (from func in base.MetaData.SubFunction
where func.FunctionId == functionId
select func).Select<SubFunctionEntity, double?>(System.Linq.Expressions.Expression.Lambda<Func<SubFunctionEntity, double?>>(System.Linq.Expressions.Expression.Multiply(System.Linq.Expressions.Expression.Property(expression = System.Linq.Expressions.Expression.Parameter(typeof(SubFunctionEntity), "func"), (MethodInfo) methodof(SubFunctionEntity.get_Volume)), System.Linq.Expressions.Expression.Property(expression, (MethodInfo) methodof(SubFunctionEntity.get_RialAmount))), new ParameterExpression[] { expression })).Sum();
}
return (from func in base.MetaData.SubFunction
where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
select func).Select<SubFunctionEntity, double?>(System.Linq.Expressions.Expression.Lambda<Func<SubFunctionEntity, double?>>(System.Linq.Expressions.Expression.Multiply(System.Linq.Expressions.Expression.Property(expression = System.Linq.Expressions.Expression.Parameter(typeof(SubFunctionEntity), "func"), (MethodInfo) methodof(SubFunctionEntity.get_Volume)), System.Linq.Expressions.Expression.Property(expression, (MethodInfo) methodof(SubFunctionEntity.get_RialAmount))), new ParameterExpression[] { expression })).Sum();
}
p.s another error is SubFunctionEntity class has a "Volume" property, and I don't understand why this code is calling some kind of static property on it like: SubFunctionEntity.get_Volume
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过ILSpy?它甚至有一些选项来控制您是否想要生成 LINQ“函数”语法或
from...where
语法(View->Options->Decompiler->Decompile query expression
和反编译匿名方法/lambdas
)对于您的其他问题:
SubFunctionEntity.get_Volume
是类的属性Volume
的 getter子函数实体
。(MethodInfo) methodof(SubFunctionEntity.get_Volume)
与typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()
相同,仅在编译时完成,而不是在运行时。问题是 C# 没有“函数”方法。如果您确实想使用 Reflector 提供的内容,这是“更正”版本:
请注意,您需要一些使用:
啊...最复杂的表达式是:
func =>; (func.Volume * func.RialAmount)
因此您可以将该方法编写为:附录:检查后,ILSpy 会产生与 Reflector 类似但不同的随机垃圾
Have you tried ILSpy? It even has some options to control if you want to produce LINQ "function" syntax or
from... where
syntax (View->Options->Decompiler->Decompile query expression
andDecompile anonymous methods/lambdas
)For your other question:
SubFunctionEntity.get_Volume
is the getter of the propertyVolume
of the classSubFunctionEntity
.(MethodInfo) methodof(SubFunctionEntity.get_Volume)
is the same astypeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()
, only done at compile time instead that at runtime. The problem is that C# doesn't have amethodof
"function".If you really want to use what the Reflector gave you, this is the "corrected" version:
Note that you'll need some using:
Ah... the most complex expression is :
func => (func.Volume * func.RialAmount)
so you could write the method as:Addendum: checked, the ILSpy produce random garbage similar-but different to Reflector