How to make your own system. In C#, what are Linq.Expression classes?

c# expression-trees

Question

Can you design your own Expression classes? Does anybody have any instances of this, if so? Since the constructor of the framework's abstract Expression class accepts an ExpressionType argument, an enum type, I can't clearly customize this, which makes inheriting from it problematic.

Any thoughts?

1
4
6/11/2009 2:37:05 PM

Expert Answer

The DLR code on Codeplex had to do this identical task; ultimately, they had to recreate the whole codebase (IIRC) till version 4.0 is released.

I haven't really tested, but this may not work well with the C# compiler.

1
6/11/2009 2:43:46 PM

Popular Answer

Because the Expression constructor in.net 3.5 accepts an ExpressionType enum value and you are unable to add additional node types to the enum, you are unable to do this.

Additionally, since all of the "leaf classes" (such as BinaryExpression) are sealed, you cannot inherit from any of them and hence cannot overload an existing node type.

If you use a node type of ExpressionType.Extension and override the "Reduce" function, it seems that you may do this with CLR version 4 according to the MSDN documentation.



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow