11

I'm playing and learning little with ANTLR building a simple DSL for .NET, transforming the script in string into Dynamic Method. My first idea was translate to IL opcodes, but now I am reading about expression trees for DM creation. It seems that I can produce any method using expression trees, just like IL code?

thanks!

Code: https://github.com/ricardoborges/NPortugol2

Ricardo
  • 1,549
  • 2
  • 12
  • 11
  • You won't be able to define new types with expression trees. IL is a way much more flexible (but yet very easy to generate). – SK-logic Feb 01 '13 at 11:32

1 Answers1

9

IL Code gives you a little more power. For example, you can use it to emit code that's correct for the .NET CLR but not necessarily something you could have written in C# or VB.NET.

But in general yes, you should be able to do just about anything you want to do with expression trees, and they are much simpler to wrap your head around.

Emitting IL code will probably run somewhat faster than compiling an expression tree. I wouldn't worry about this (premature optimization), but it's worth noting. See How does having a dynamic variable affect performance? for some performance specs.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 2
    One significant difference between the two is that IL can be used to create whole classes and assemblies. Expressions can be used only to create methods. – svick Feb 01 '13 at 02:35