Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I finally found a solution that meets my needs here, and what do you know - it involves LINQ.</p> <p>A blog post on .NET mentioned offhand how you can convert any expression lambda into an Expression&lt;> object if you know the result type of the expression. Given that, it turns out that you can take an expression like this:</p> <p>() => this.Foo.Bar</p> <p>And walk through the nodes to end up with a MemberInfo (for 'Bar') and a 'this' object (for 'this.Foo'). Given these two values, you can bind directly to a field or property specified at compile time. In the case of a property, you can do this very efficiently by retrieving the Get and Set methods of the property from the MemberInfo and converting them into strongly-typed delegates, making the cost of reading/writing that property very low.</p> <p>Best of all, this works perfectly with automated refactoring and source code searches because it's putting the compiler's muscle to use for you, and it doesn't require any modifications to existing code in order to allow binding.</p> <p>The only downside is that it's very difficult to bind to fields/properties of a struct, but I don't consider that a particularly relevant use case anyway.</p> <p>Anyone looking to solve this or a similar problem can view the source code here:</p> <p><a href="http://code.google.com/p/fracture/source/browse/trunk/Squared/Util/Bind.cs" rel="nofollow noreferrer">http://code.google.com/p/fracture/source/browse/trunk/Squared/Util/Bind.cs</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload