打印出 Linq 表达式树层次结构

发布于 2024-07-16 06:06:55 字数 845 浏览 11 评论 0原文

动态语言运行时 (DLR) 有一些非常酷的 Expression 代码,包括一些非常好的代码来打印我想要使用的表达式树,以便:

int a = 1;
int b = 2;
Expression<Func<int, int>> expression = (c) => a + (b * c)
expression.Evaluate(5, stringBuilder)

输出:

(5) => a + (b * c) = 11 Where
     a = 1
     b * c = 10 Where
          b = 2
          c = 5

我在网上找到了一些代码来执行此操作,但发现它仅在表达式不带参数时才有效。

http://incrediblejourneysintotheknown.blogspot.com/2009/02 /displaying-nested-evaluation-tree-from.html

然后我发现了类似方法的 DLR 实现。 然而,DLR 有自己的 Expression 类和许多其他标准 C# 类型的自定义实现,所以我有点困惑。 有人知道我如何实现上述内容吗?

The dynamic language runtime (DLR) has some pretty cool code for Expression's, including some very nice code to print out Expression trees which I want to use so that:

int a = 1;
int b = 2;
Expression<Func<int, int>> expression = (c) => a + (b * c)
expression.Evaluate(5, stringBuilder)

Outputs:

(5) => a + (b * c) = 11 Where
     a = 1
     b * c = 10 Where
          b = 2
          c = 5

I found some code on the net to do this but found that it only works if the expressiontakes in no arguments.

http://incrediblejourneysintotheknown.blogspot.com/2009/02/displaying-nested-evaluation-tree-from.html

I then discovered the DLR implementation of a similar method. However the DLR has its own custom implementations of the Expression class and many other standard C# types so I got a little confused. Anyone know how I can implement the above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

画尸师 2024-07-23 06:06:55

怎么样:

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;

static class Program
{
    static void Main()
    {
        int a = 1, b = 2;
        StringBuilder sb = new StringBuilder();
        Expression<Func<int, int>> expression = (c) => a + (b * c);
        expression.Evaluate(sb, 5);
        // now fix the capture class names (from a and b)
        string s = sb.ToString();
        s = Regex.Replace(s, @"value\([^)]+\)\.", "");
        Console.WriteLine(s);
    }
    public static void Evaluate(this LambdaExpression expr, StringBuilder builder, params object[] args)
    {
        var parameters = expr.Parameters.ToArray();
        if (args == null || parameters.Length != args.Length) throw new ArgumentException("args");
        Evaluate(expr.Body, 0, builder, parameters, args);
    }
    private static StringBuilder Indent(this StringBuilder builder, int depth)
    {
        for (int i = 0; i < depth; i++) builder.Append("  ");
        return builder;
    }
    private static void Evaluate(this Expression expr, int depth, StringBuilder builder, ParameterExpression[] parameters, object[] args)
    {
        builder.Indent(depth).Append(expr).Append(" = ").Append(Expression.Lambda(expr, parameters).Compile().DynamicInvoke(args));

        UnaryExpression ue;
        BinaryExpression be;
        ConditionalExpression ce;

        if ((ue = expr as UnaryExpression) != null)
        {
            builder.AppendLine(" where");
            Evaluate(ue.Operand, depth + 1, builder, parameters, args);
        }
        if ((be = expr as BinaryExpression) != null)
        {
            builder.AppendLine(" where");
            Evaluate(be.Left, depth + 1, builder, parameters, args);
            Evaluate(be.Right, depth + 1, builder, parameters, args);                   
        }
        else if ((ce = expr as ConditionalExpression) != null)
        {
            builder.AppendLine(" where");
            Evaluate(ce.Test, depth + 1, builder, parameters, args);
            Evaluate(ce.IfTrue, depth + 1, builder, parameters, args);
            Evaluate(ce.IfFalse, depth + 1, builder, parameters, args);
        }
        else
        {
            builder.AppendLine();
        }
    }

}

How about:

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;

static class Program
{
    static void Main()
    {
        int a = 1, b = 2;
        StringBuilder sb = new StringBuilder();
        Expression<Func<int, int>> expression = (c) => a + (b * c);
        expression.Evaluate(sb, 5);
        // now fix the capture class names (from a and b)
        string s = sb.ToString();
        s = Regex.Replace(s, @"value\([^)]+\)\.", "");
        Console.WriteLine(s);
    }
    public static void Evaluate(this LambdaExpression expr, StringBuilder builder, params object[] args)
    {
        var parameters = expr.Parameters.ToArray();
        if (args == null || parameters.Length != args.Length) throw new ArgumentException("args");
        Evaluate(expr.Body, 0, builder, parameters, args);
    }
    private static StringBuilder Indent(this StringBuilder builder, int depth)
    {
        for (int i = 0; i < depth; i++) builder.Append("  ");
        return builder;
    }
    private static void Evaluate(this Expression expr, int depth, StringBuilder builder, ParameterExpression[] parameters, object[] args)
    {
        builder.Indent(depth).Append(expr).Append(" = ").Append(Expression.Lambda(expr, parameters).Compile().DynamicInvoke(args));

        UnaryExpression ue;
        BinaryExpression be;
        ConditionalExpression ce;

        if ((ue = expr as UnaryExpression) != null)
        {
            builder.AppendLine(" where");
            Evaluate(ue.Operand, depth + 1, builder, parameters, args);
        }
        if ((be = expr as BinaryExpression) != null)
        {
            builder.AppendLine(" where");
            Evaluate(be.Left, depth + 1, builder, parameters, args);
            Evaluate(be.Right, depth + 1, builder, parameters, args);                   
        }
        else if ((ce = expr as ConditionalExpression) != null)
        {
            builder.AppendLine(" where");
            Evaluate(ce.Test, depth + 1, builder, parameters, args);
            Evaluate(ce.IfTrue, depth + 1, builder, parameters, args);
            Evaluate(ce.IfFalse, depth + 1, builder, parameters, args);
        }
        else
        {
            builder.AppendLine();
        }
    }

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