Daigo Hamura's Weblog

Expression Tree Compiler (Expression Tree Advnaced)

Expresion Tree は前回書いたように .NET Assembly に書き出された Data Structure です。 DLINQはその Expression Tree をもとに SQL に変換しデータベースにクエリを投げます。 しかし、アプリケーションによっては、Expression Tree をデータ として使いかつ、CLRの実行結果を知りたいときがあります。  このような時に役に立つのが、Expression Tree Compilerです。 Expression Tree を Delegate にコンパイルする事によって、通常の Delegate として Invoke できるようになります。

Expression<Func<int, bool>> expr = x => x > 10;
Func<int, bool> func = expr.Compile();  // Expression Tree から Delegate へコンパイル

// Console.WriteLine(expr(1)); // Compile Time Error - cannot invoke expr
Console
.WriteLine(func(1));  
Console.WriteLine(func(10));
Console.WriteLine(func(11));

次回は、この Expression Tree コンパイラが、実際どこに使われているかをDLINQの例をとって説明したい思います。

Published Monday, June 12, 2006 1:54 PM by daigoh
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker