C#4.0:表达式树主场迎战的CodeDOM [英] C# 4.0: Expression trees vs. CodeDom

查看:153
本文介绍了C#4.0:表达式树主场迎战的CodeDOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是表达式树和CodeDom中之间的区别是什么?
我应该使用哪种对哪种方案?

What are the differences between Expression trees and CodeDom? Which should I use for which scenario?

推荐答案

表达式树有很多共同点(例如)一 AST 的。它不直接映射到代码,但是很适合于从算法结构。例如,如果你正在解析公式:

Expression trees have a lot in common with (for example) an AST. It doesn't map directly to code, but is very amenable to construction from algorithms. For example, if you are parsing a formula:

((a + 2) / b)

这就是:

ParameterExpression a = ..., b = ...
var body = Expression.Divide(
    Expression.Add(a, Expression.Constant(2)),
    b);
var lambda = Expression.Lambda(body,a,b); // optionally with generics



其实,我做的究竟的这一点,使用构建对象树,对象通过一个访客的实施产生完整的中表达的分析器。在.NET 4.0中,更丰富的表达树的支持使得它可以支持大多数情况下,编译它的需求。

In fact, I have done exactly this, using a parser that build an object tree, with objects generating the complete expresion via a "visitor" implementation. In .NET 4.0, the richer expression-tree support makes it possible to support most scenarios, and compile it on demand.

表现的另一个重要用途是,你可以<在运行时EM>解构的他们,这样的在你的代码你可能有:

Another key use of expressions is that you can deconstruct them at runtime, so in your code you might have:

Foo(x => x.SomeMethod(1, "abc"));

和提取的someMethod 方法,信息, 1 ABC等。

and extract the SomeMethod method-info, 1 and "abc" etc.

CodeDOM的映射到的代码即可。它是所有关于报表等,非常类似于你会怎么写正代码。 CodeDOM的最常见的用途是用于代码生成,作为模具的一部分。您的可以的使用它的动态编译,但说实话这是很难。我不是一个风扇。在不错的功能是一个的CodeDOM树的可能的多国语言的工作。

codedom maps to code. It is all about statements etc, very similar to how you would write regular code. The most common use of codedom is for code generation, as part of tooling. You can use it for dynamic compilation, but to be honest it is harder. I am not a fan. The nice feature is that a codedom tree might work for multiple languages.

另外这里的竞争者应 DynamicMethod的和/或的ILGenerator 。这映射到一个AST(表情),而不能用来生成源代码(CodeDOM的),但允许完全访问MSIL的工具。当然,它也需要你想堆等方面,但它的非常高效和有效的元编程。

Another contender here should be DynamicMethod and/or ILGenerator. This doesn't map to an AST (expression), and can't be used to generate source-code (codedom), but allows full access to the MSIL tools. Of course, it also requires that you think in terms of stacks etc, but it is very efficient and effective for meta-programming.

如果的ILGenerator 太硬核,并且CodeDOM的是PITA,那么另一个选择是运行时生成代码的作为字符串。然后传递到 CSharpCodeProvider 进行编译。有迹象表明,这样做(的XmlSerializer IIRC)。

If ILGenerator is too hard-core, and codedom is a PITA, then another option is runtime generation of code as a string. Then pass that through CSharpCodeProvider to compile it. There are parts of the core runtime that do this (XmlSerializer IIRC).

总结一下:


  • 元编程:的ILGenerator CSharpCodeProvider ;还表达式 4.0(但这是在3.5相当有限)

  • 处理AST:表达式

  • 解析在运行时:表达式

  • 代码生成的多国语言:代码-DOM

  • meta-programming: ILGenerator or CSharpCodeProvider; also Expression in 4.0 (but this is quite limited in 3.5)
  • handling AST: Expression
  • parsing at runtime: Expression
  • code-generation in multiple languages: code-dom

这篇关于C#4.0:表达式树主场迎战的CodeDOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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