Member Override
Have you ever found yourself writing
Despite taking up a lot of space it doesn’t do anything terribly exciting. It just temporarily overrides the value of a variable, then after we finished doing some operations we set it back to what it was before we messed with it.
So why would anyone want to do this? Primarily i have used such code to fudge frameworks that depend heavily on static variables, i can override static variables to temporarily modify the behavior of my static-heavy framework and trick it into doing what i want. I’m sure there are other less devious (and more thread safe) uses of such code but it is the coding hacks that readily come to mind.
Every time I find myself writing such code I always get turned off by the amount of extra space it adds, that’s 9 additional lines in my method that just obfuscate my codes intention. I would extract the temporary assignment into its own method, but coming up with intuitive parameters that would work was too difficult
… well that was the way i thought until i learned the power of C# Expressions
Enter the Expression
Expressions are the hidden gem in C#. They allow you to pass what looks like an innocent lambda expression into a method, but inside your method you get a full expression tree that represents everything your lambda function is doing. This includes being able to read what members your lambda is reading, what methods are being called, any arithmatic operators used, the whole lot completely visible in your method.
This is very different to our normal understanding of how code is executed, normally if I pass something to a method only the final result it passed. But expressions pass the code that is used to generate the result. We can then examine the code passed and makes decisions on the code, not the values.
How does one write one of these mighty methods, well suprisingly it is very easy
then you might think the difficulty is in calling this amazing method, how is one going to pass an Expression<Func<int,int»? Well it turns out thats even easier than the method definition
Yes it looks just like you passed a regular delegate to the method! you have probably been calling Expression methods all this time but never realised, they have been hiding under the guise of regular lambda expressions.
How can I harness such power
Back to my value overriding, would it not be great to have something like
(it would be greater to have the assignment in the expression as well, but expressions don’t like assignments in them)
Using unexpressioned methods such a method would be impossible, all we pass it is a delegate to read the value and the value we want it to be, our method would have no idea how to set the value. But with expressions we know what member is being used so we can use that to both get and set the value.
So how does such a method work
Most of the code is just allowing us to get/set values on both properties and fields, the actual code we are interested in is the Set
method. Firstly we take an expression and convert it into the member it is accessing and another expression of the baseObject
the method is being called on. To convert our baseObject
from an expression to a real object we have to compile it. Once we have the member and the baseObject
its just a matter of using reflection to set/get the values
Was all the expressions really necassary
In the above code I have also included the non expression version, it takes one extra parameter and doesn’t look as cool when called, but its code is significantly shorter and executes a whole lot faster. So should we be using expressions to do this?
Ill let you make the call, either way it is an interesting piece of code and its a a good gateway into the wonderful world of expressions.