在EX pression树木落实不,.NET 4 [英] Implement Not in expression trees, .net 4

查看:159
本文介绍了在EX pression树木落实不,.NET 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能实现! (不)使用EX pression树。我感兴趣的创建,将分析和评估逻辑EX pressions含真,假,||,&放一个C#的eval类;&安培;和 !。我知道,与功放;&安培;和||目前由.NET 4 EX pression树木的支持,但我想知道如果他们的是一种方法来实现类似summat!(X放大器;&安培; Y)||当z =假,Y =真和z =假的。

Is it possible to implement a ! (not) using expression trees. I'm interested in creating a C# eval class which will parse and evaluate logical expressions which contain true, false, ||, && and !. I know that && and || are currently supported by .NET 4 expression trees, but I was wondering if their is a way to implement summat like !(x && y) || z where z=false, y=true and z=false.

目前我使用的是标准的基于堆栈的标记生成器,分析器,评估,以评估这些类型的EX pression,但会happly倾倒,如果有合适的EX pression树可以创建和动态执行

Currently I'm using a standard stack based tokenizer, parser, evaluator to evaluate these types of expression but would happly dump it, if a suitable expression tree could be created and executed on the fly.

推荐答案

我通常会发现,它的价值编码的lambda EX pression这确实是我想要的,然后看一下C#编译器吧。

I usually find that it's worth coding a lambda expression which does what I want, and then seeing what the C# compiler does with it.

所以这个code:

Expression<Func<bool,bool>> func = x => !x;

被编译为:

ParameterExpression expression2;
Expression<Func<bool, bool>> expression = 
       Expression.Lambda<Func<bool, bool>>(Expression.Not(
            expression2 = Expression.Parameter(typeof(bool), "x")), 
                new ParameterExpression[] { expression2 });

(道歉格式 - 这是很难知道该怎么做与)

(Apologies for the formatting - it's hard to know what to do with that.)

这是反编译带反射,但它的优化设置为.NET 2.0,以避免使用lambda语法。

That's decompiled with Reflector, but with its "optimization" set at .NET 2.0 to avoid it using lambda syntax.

一旦你过去的垃圾,你会看到它使用<一个href="http://msdn.microsoft.com/en-us/library/system.linq.ex$p$pssions.ex$p$pssion.not.aspx"><$c$c>Ex$p$pssion.Not. &功放;&安培;使用<一个href="http://msdn.microsoft.com/en-us/library/system.linq.ex$p$pssions.ex$p$pssion.andalso.aspx"><$c$c>Ex$p$pssion.AndAlso,和||使用<一个href="http://msdn.microsoft.com/en-us/library/system.linq.ex$p$pssions.ex$p$pssion.orelse.aspx"><$c$c>Ex$p$pssion.OrElse.

Once you've got past the junk, you'll see it's using Expression.Not. && uses Expression.AndAlso, and || uses Expression.OrElse.

这篇关于在EX pression树木落实不,.NET 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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