.NET Framework 4.0 Beta1里的Expression Tree一例2010-02-05 javaeye.com RednaxelaFX既然装上了Visual Studio 2010 Beta 1,正好可以试试.NET Framework 4.0里的一些新东西。我比较关注的是Expression Tree的部分,到底哪些功能进到了.NET 4,哪些还得到CodePlex的DLR站上去找呢?试用一下找找感觉。我暂时没试这个beta里的C#对dynamic的支持,是因为暂时还没想到啥有趣的场景能写点简单的代码来玩的。对.NET类型故意使用dynamic的玩法在之前CTP的时候就玩过了,不过瘾了。回头针对.NET 4来编译一个IronPython来看看,到时就能好好把玩一番dynamic了。回到Expression Tree。在.NET Framework 4.0里它叫Expression Tree v2,简称ETv2。它兼容于.NET Framework 3.5里LINQ的Expression Tree,但实际上是从DLR的DLR tree发展而来的。时至今日DLR的代码仍在快速变化中,而ETv2作为LINQ与DLR的公共部分要放到标准库里,等不到DLR稳定下来。折中的解决方案就是在标准库里的版本砍掉一些DLR里还没定下来的东西和低优先级的东西。像是LoopExpression进入了标准库,但特化版本的ForEach、While等就只有CodePlex上的版本才有。.NET Framework 4.0中,ETv2位于System.Core.dll程序集中,在System.Linq.Expressions命名空间下。CodePlex的DLR的ETv2则位于Microsoft.Scripting.Core.dll程序集中,Microsoft.Linq.Expressions命名空间下。CodePlex的DLR之所以要用不同的命名空间是为了避免与标准库冲突,但这样一来由编译器生成的ET就与CodePlex的DLR中的ET不兼容了。所以我才那么期待.NET 4.0赶紧出……为了能用上标准库里的ETv2。昨天装好VS2010 Beta后写的代码如下。

就是先做了个简单的in-memory LINQ查询,然后用ETv2来构造出一个遍历并输出查询结果的函数,并调用之。C#代码
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;namespace ConsoleApplication1 {static class Program {static void Main(string[] args) {var list = from i in Enumerable.Range(0, 100) where i % 9 == 0 orderby i descending select i;var vIter= Expression.Variable(typeof(IEnumerator<int>), "iter");var vI = Expression.Variable(typeof(int), "i");var lBreak = Expression.Label();var eForeach = Expression.Lambda<Action>(Expression.Block(new[] { vIter, vI }, // IEnumerator<int> iter; int i;Expression.Assign( // iter = list.GetEnumerator();vIter,Expression.Call(Expression.Constant(list),typeof(IEnumerable<int>).GetMethod("GetEnumerator"))),Expression.Loop( // while (true)Expression.IfThenElse( // ifExpression.Call( // (iter.MoveNext())vIter,typeof(IEnumerator).GetMethod("MoveNext")),Expression.Block(// {Expression.Assign( // i = iter.Current;vI,Expression.Property(vIter, "Current")),Expression.Call( // Console.WriteLine(i);typeof(Console).GetMethod("WriteLine", new[] { typeof(int) }),new[] { vI })),Expression.Break(lBreak)), // } else break; }lBreak)),new ParameterExpression[0]);eForeach.Compile()();}}}