表达式树的ToString()方法生成奇怪的字符串,为什么? [英] Expression Tree ToString() method generates strange string WHY?

查看:78
本文介绍了表达式树的ToString()方法生成奇怪的字符串,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要表达式树的字符串转换,所以

I need string conversion of an Expression Tree so

我创建一个表达式树并使用ToString方法,像这样

I create an Expression Tree and use ToString method like this

var exp = ((Expression<Func<UserDetailInfo, bool>>) (x => x.OperationID == operationId)).ToString();

但是结果很奇怪

x => (x.OperationID == value(TCS.Proxy.PermissionProxy+<>c__DisplayClass5).operationId)

TCS.Proxy.PermissionProxy是我在WCF代理项目中的类!!! (我将表达式从客户端发送到代理)

TCS.Proxy.PermissionProxy is my class in WCF proxy project !!! (I send expression from client to proxy)

但是当我自己创建此表达式时,一切都很好

but when I create this Expression myself everything is good

var entity = Expression.Parameter(typeof(UserDetailInfo));
var constant = Expression.Constant(operationId);
var e = Expression.Equal(Expression.Property(entity, "OperationID"), constant);
var exp = Expression.Lambda<Func<UserDetailInfo, bool>>(e, entity).ToString();

结果还可以

Param_0 => (Param_0.OperationID == operationId) // I Need this

我如何使用ToString()可以生成如上的结果?

How I can use ToString() can generates result like above ?

为什么两个结果不同?

Why two results is different ?

* 我将表达式转换为字符串,以便将其从客户端传输到WCF服务,因此我需要正确的字符串,以便在服务器端将字符串从字符串转换为表达式

* I Convert Expression to String for transfer it from client to WCF service so I need correct string for convert in server side from string to Expression

推荐答案

这是因为您的右手侧成员不是常量,而是捕获的变量. TCS.Proxy.PermissionProxy+<>c__DisplayClass5部分意味着在类TCS.Proxy.PermissionProxy中,它必须创建5个新类,这些新类包含通过变量捕获传递的值,并且该特定的lambda使用它创建的第5个.

This is because your right hand side member is not a constant, it is a captured variable. The TCS.Proxy.PermissionProxy+<>c__DisplayClass5 part means in the class TCS.Proxy.PermissionProxy it had to create 5 new classes that holds values that where passed in via variable capture and this specific lambda uses the 5th one it created.

您的代码(您从未展示过您的功能,所以我做了一些猜测)

Your code (You never show your function so I made some guesses)

namespace TCS.Proxy
{
    class PermissionProxy
    {
         public void SomeFunction()
         {
             int operationId = 0;
             var exp = ((Expression<Func<UserDetailInfo, bool>>) (x => x.OperationID == operationId)).ToString()
         }
    }
}

正在被重写为类似的东西(实际上有很大的不同,但是本示例说明了这一点)

Is getting re-written to something similar (It's actually a lot different but this example gets the point across) to

namespace TCS.Proxy
{
    public class PermissionProxy
    {

         private class c__DisplayClass5
         {
             public int operationId;
         }

         public void SomeFunction()
         {
             int operationId = 0;

             var <>c__DisplayClass5 = new c__DisplayClass5();
             <>c__DisplayClass5.operationId = operationId;

             var exp = ((Expression<Func<UserDetailInfo, bool>>) (x => x.OperationID == <>c__DisplayClass5.operationId)).ToString()
         }
    }
}

与您手动创建的内容不同.如果要取消装箱"这些自定义类,则需要编写 ExpressionVisitor ,它将遍历表达式并将其重新编写为您要遍历的表单.

Which is different than what you manually created. If you want to "unbox" these custom classes you will need to write up a ExpressionVisitor that will go through the expression and re-write it in to the form you want to go over the wire.

这篇关于表达式树的ToString()方法生成奇怪的字符串,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆