Reflector生成Lambda表达式,如何取回实际的Linq查询?

发布于 2024-12-10 04:51:47 字数 1910 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-12-17 04:51:47

您是否尝试过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 提供的内容,这是“更正”版本:

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?>(Expression.Lambda<Func<SubFunctionEntity, double?>>(Expression.Multiply(Expression.Property(expression = Expression.Parameter(typeof(SubFunctionEntity), "func"), typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()), Expression.Property(expression, typeof(SubFunctionEntity).GetProperty("RialAmount").GetGetMethod())), new ParameterExpression[] { expression })).Sum();
    }
    return (from func in base.MetaData.SubFunction
            where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
            select func).Select<SubFunctionEntity, double?>(Expression.Lambda<Func<SubFunctionEntity, double?>>(Expression.Multiply(Expression.Property(expression = Expression.Parameter(typeof(SubFunctionEntity), "func"), typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()), Expression.Property(expression, typeof(SubFunctionEntity).GetProperty("RialAmount").GetGetMethod())), new ParameterExpression[] { expression })).Sum();
}

请注意,您需要一些使用:

using System.Linq;
using System.Linq.Expressions;

啊...最复杂的表达式是: func =>; (func.Volume * func.RialAmount) 因此您可以将该方法编写为:

public double? LoadSumRialAmount(long functionId, long? subFunctionId)
{
    if (!subFunctionId.HasValue)
    {
        return (from func in base.MetaData.SubFunction
                where func.FunctionId == functionId
                select func).Select(func => (func.Volume * func.RialAmount)).Sum();
    }
    return (from func in base.MetaData.SubFunction
            where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
            select func).Select(func => (func.Volume * func.RialAmount)).Sum();
}

附录:检查后,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 and Decompile anonymous methods/lambdas)

For your other question: SubFunctionEntity.get_Volume is the getter of the property Volume of the class SubFunctionEntity.

(MethodInfo) methodof(SubFunctionEntity.get_Volume) is the same as typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod(), only done at compile time instead that at runtime. The problem is that C# doesn't have a methodof "function".

If you really want to use what the Reflector gave you, this is the "corrected" version:

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?>(Expression.Lambda<Func<SubFunctionEntity, double?>>(Expression.Multiply(Expression.Property(expression = Expression.Parameter(typeof(SubFunctionEntity), "func"), typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()), Expression.Property(expression, typeof(SubFunctionEntity).GetProperty("RialAmount").GetGetMethod())), new ParameterExpression[] { expression })).Sum();
    }
    return (from func in base.MetaData.SubFunction
            where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
            select func).Select<SubFunctionEntity, double?>(Expression.Lambda<Func<SubFunctionEntity, double?>>(Expression.Multiply(Expression.Property(expression = Expression.Parameter(typeof(SubFunctionEntity), "func"), typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()), Expression.Property(expression, typeof(SubFunctionEntity).GetProperty("RialAmount").GetGetMethod())), new ParameterExpression[] { expression })).Sum();
}

Note that you'll need some using:

using System.Linq;
using System.Linq.Expressions;

Ah... the most complex expression is : func => (func.Volume * func.RialAmount) so you could write the method as:

public double? LoadSumRialAmount(long functionId, long? subFunctionId)
{
    if (!subFunctionId.HasValue)
    {
        return (from func in base.MetaData.SubFunction
                where func.FunctionId == functionId
                select func).Select(func => (func.Volume * func.RialAmount)).Sum();
    }
    return (from func in base.MetaData.SubFunction
            where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
            select func).Select(func => (func.Volume * func.RialAmount)).Sum();
}

Addendum: checked, the ILSpy produce random garbage similar-but different to Reflector

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文