Viewing the IL code generated from a compiled expression
Yes! Use this tool: https://github.com/drewnoakes/il-visualizer This was incredibly useful when I was implementing and debugging Compile, as I’m sure you can imagine.
Yes! Use this tool: https://github.com/drewnoakes/il-visualizer This was incredibly useful when I was implementing and debugging Compile, as I’m sure you can imagine.
I had the same problem, but somewhat more complex, and Darin Dimitrov’s answer gave me a good start. I’ll post my results here, despite the fact that this is an “old” question . Case 1: The root object is an instance member this.textBox.Text // where ‘this’ has type ‘Form’ … is equivalent to the following … Read more
As noted in comments, the problem is that there exists no column in the reader for the specified property. The idea is to loop by the column names of reader first, and check to see if matching property exists. But how do one get the list of column names beforehand? One idea is to use … Read more
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
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
Here’s a visitor that does the job. it makes a copy of the parameter (since we’ll need to create a new parameter and substitute all references of the old parameter for the new one) it walks the .Body of the tree, substituting the parameter, and switching any member-access against the old type to a like-named … Read more
Based on the answer by Arthur I’ve create a working wrapper. The snippets provided provide a way to wrap each LINQ query with your own QueryProvider and IQueryable root. This would mean that you’ve got to have control over the initial query starting (as you’ll have most of the time using any sort of pattern). … Read more
This is pretty strange for such a huge overheard. There are a few things to take into account. First the VS compiled code has different properties applied to it that might influence the jitter to optimize differently. Are you including the first execution for the compiled delegate in these results? You shouldn’t, you should ignore … Read more
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
So the starting place is creating an expression visitor. This lets us find all of the member accesses within a particular expression. This leaves us with the question of what to do for each member access. So the first thing is to recursively visit on the expression that the member is being accessed on. From … Read more