1

I ask because I'm trying to back port something to use with unity which uses C# 2.0. e.g. I have a line like this:

var patternSymbol = Expression.Parameter(typeof (T));

So can I substitute a bit of my own code to make this work? Note: I can change stuff around as I like but the further I get away from the original code the more work I'm likely to run into. Essentially I need to replicate the Expression/Parameter behaviour (from C# 3.0?) in C# 2.0.

(I'm a bit of a C# newbie and finding it difficult to find answers to what are now historical questions like this).

demented hedgehog
  • 7,007
  • 4
  • 42
  • 49
  • 2
    No there is none. But if you can post what that code does with `patternSymbol` we can help you with implementing the same in .Net 2.0. Your compiler error suggests that there is no `Parameter` method in `Expression` but there exist an `Expression` class; but there shouldn't be a `Expression` class in .net 2.0. Are you sure you're not getting any other error? If so where does that Expression class comes from? – Sriram Sakthivel Feb 01 '15 at 08:34
  • yeah. You're right. I added an empty Expression class a some hours ago to get this far. Stupid of me not to recall. I'll try and modify the question to make sense in the current context. Thanks Sriram. – demented hedgehog Feb 01 '15 at 08:43
  • 1
    Trying to reimplement expression trees in .NET 2.0 is going to be a *lot* of work. I think there's a project that did it somewhere, but I wouldn't be surprised if it used other aspects of .NET 2.0 which aren't available in Unity. I would strongly advise trying to redesign to not *need* expression trees. – Jon Skeet Feb 01 '15 at 08:50
  • Thanks Jon. Sometimes you don't know how much work things are going to be until you wade in. I'll give up on this approach. – demented hedgehog Feb 01 '15 at 13:04
  • The project which backports Expressions (among other things) to .net 2.0 is [Theraot's Libraries](https://github.com/theraot/Theraot). I was able to compile dotNetLiquid for .net 2.0 using it, and all their unit tests pass successfully. If you are curios about other useful backports - check out [this SO question](http://stackoverflow.com/q/3462419/2112891). – anikiforov Apr 27 '15 at 18:40

1 Answers1

1

You cannot backport this code to .NET 2.0 because the entire LINQ subsystem is missing. System.Linq.Expressions.Expression class has been introduced as part of LINQ with C# 3.5, which was a big step forward for the .NET framework.

In particular, Expression.Parameter is not useful all by itself. Typically, you would find Expression.Lambda and a Compile calls further down the code, followed by a cast to Func<...>. Imitating these calls would require generating CLR code, which is almost like writing your own compiler. It's rarely worth the effort, though.

You need to see how the old code is used, and build a replacement from scratch. For example, you could replace a compiled expression with an interpreted one, which is considerably easier to build (at the price of lower run-time performance). In any event, you would end up writing an entirely new subsystem, not simply porting the code.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523