Expression Trees (C# and Visual Basic)

https://msdn.microsoft.com/en-us/library/bb397951.aspx

Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y.

 

You can compile and run code represented by expression trees.

This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries.

For more information about expression trees in LINQ, see How to: Use Expression Trees to Build Dynamic Queries (C# and Visual Basic).

 

Expression trees are also used in the dynamic language runtime (DLR) to provide interoperability between dynamic languages and the .NET Framework and to enable compiler writers to emit expression trees instead of Microsoft intermediate language (MSIL).

For more information about the DLR, see Dynamic Language Runtime Overview.

 

You can have the C# or Visual Basic compiler create an expression tree for you based on an anonymous匿名的 lambda expression,

or you can create expression trees manually by using the System.Linq.Expressions namespace.

 

Creating Expression Trees from Lambda Expressions

When a lambda expression is assigned to a variable of type Expression<TDelegate>, the compiler emits发出,放射;发行;发表 code to build an expression tree that represents the lambda expression.

 

The C# and Visual Basic compilers can generate expression trees only from expression lambdas (or single-line lambdas).

It cannot parse statement lambdas (or multi-line lambdas).

For more information about lambda expressions in C#, see Lambda Expressions (C# Programming Guide);

for Visual Basic, see Lambda Expressions (Visual Basic).

 

The following code examples demonstrate how to have the C# and Visual Basic compilers create an expression tree that represents the lambda expression num => num < 5 (C#) or Function(num) num < 5 (Visual Basic).

Expression<Func<int, bool>> lambda = num => num < 5;

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(467)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示