Declaring Func dynamically

You can do this by using an open generic type definition, and then making the specific type from that: typeof(Func<,>).MakeGenericType(typeof(int), orderType); However, what you’re trying to do (calling Lambda<TDelegate>) is not directly possible. You must call Lambda without a type parameter: var propertyinfo = typeof(T).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; var param = Expression.Parameter(typeof(T), “x”); var … Read more

typesafe NotifyPropertyChanged using linq expressions

What does the code that raises this look like? I’m guessing it is something like: NotifyOfPropertyChange(() => SomeVal); which is implicitly: NotifyOfPropertyChange(() => this.SomeVal); which does a capture of this, and pretty-much means that the expression tree must be constructed (with Expression.Constant) from scratch each time. And then you parse it each time. So the … Read more

Practical use of expression trees [closed]

As Jon notes, I use them to provide generic operators with .NET 3.5. I also use them (again in MiscUtil) to provide fast access to non-default constructors (you can’t use Delegate.CreateDelegate with constructors, but Expression works fine). Other uses of manually created expression trees: object cloning dynamic LINQ sorting as a compiler But really, Expression … Read more