表达式转字符串

发布于 2024-10-25 04:33:13 字数 273 浏览 2 评论 0原文

获得一个字符串

Namespace.IMyService.Do("1")

我怎样才能从这个片段中演示的表达式中

IMyService myService = ...;
int param1 = 1;

myExpressionService.Get(c => myService.Do(param1));

:我实际上不想调用 Do ,除非使用生成的字符串满足条件。

How can I get a string like

Namespace.IMyService.Do("1")

from the expression as demoed in this snip it:

IMyService myService = ...;
int param1 = 1;

myExpressionService.Get(c => myService.Do(param1));

I actually don't want to call Do unless a condition is met using the string that was generated.

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

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

发布评论

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

评论(3

在风中等你 2024-11-01 04:33:13

您必须遍历表达式树。以下是一些示例代码:

internal static class myExpressionService
{
    public static string Get(Expression<Action> expression)
    {
        MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
        var method = methodCallExpression.Method;
        var argument = (ConstantExpression) methodCallExpression.Arguments.First();

        return string.Format("{0}.{1}({2})", method.DeclaringType.FullName, method.Name, argument.Value);
    }
}

如果以这种方式调用,它会起作用: string result = myExpressionService.Get(() => myService.Do(1));

输出为: Namespace. IMyService.Do(1)

您可以扩展/更新它来处理您的场景。

You will have to traverse Expression trees. Here is some sample code:

internal static class myExpressionService
{
    public static string Get(Expression<Action> expression)
    {
        MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
        var method = methodCallExpression.Method;
        var argument = (ConstantExpression) methodCallExpression.Arguments.First();

        return string.Format("{0}.{1}({2})", method.DeclaringType.FullName, method.Name, argument.Value);
    }
}

It works if called in this way: string result = myExpressionService.Get(() => myService.Do(1));

The output is: Namespace.IMyService.Do(1)

You can extend/update it to handle your scenario.

晨光如昨 2024-11-01 04:33:13

这也可以工作(尽管它不是特别优雅):

public static string MethodCallExpressionRepresentation(this LambdaExpression expr)
{
    var expression = (MethodCallExpression)expr.Body;

    var arguments = string.Join(", ", expression.Arguments.OfType<MemberExpression>().Select(x => {
        var tempInstance = ((ConstantExpression)x.Expression).Value;
        var fieldInfo = (FieldInfo)x.Member;
        return "\"" + fieldInfo.GetValue(tempInstance).ToString() + "\"";
    }).ToArray());

    return expression.Object.Type.FullName + "." + expression.Method.Name + "(" + arguments + ")";
}

你可以这样调用它:

Expression<Action> expr = c => myService.Do(param1));
var repr = expr.MethodCallExpressionRepresentation();    // Namespace.IMyService.Do("1")

This would also work (though it's not particularly elegant):

public static string MethodCallExpressionRepresentation(this LambdaExpression expr)
{
    var expression = (MethodCallExpression)expr.Body;

    var arguments = string.Join(", ", expression.Arguments.OfType<MemberExpression>().Select(x => {
        var tempInstance = ((ConstantExpression)x.Expression).Value;
        var fieldInfo = (FieldInfo)x.Member;
        return "\"" + fieldInfo.GetValue(tempInstance).ToString() + "\"";
    }).ToArray());

    return expression.Object.Type.FullName + "." + expression.Method.Name + "(" + arguments + ")";
}

You can call it like this:

Expression<Action> expr = c => myService.Do(param1));
var repr = expr.MethodCallExpressionRepresentation();    // Namespace.IMyService.Do("1")
胡大本事 2024-11-01 04:33:13

您应该能够对结果表达式调用 .ToString() 。根据 MS 文档,ToString() 返回表达式的文本表示形式。你尝试过吗?

You should be able to call .ToString() on the resulting expression. According to the MS documentation ToString() returns the textual representation of the expression. Have you tried that?

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