How can I get object instance from ()=>foo.Title expression

.net-3.5 c# data-binding expression-trees lambda

Question

I have a simple class with a property

class Foo 
{ 
    string Title { get; set; } 
}

I am trying to simplify data binding by calling a function like

BindToText(titleTextBox, ()=>foo.Title );

which is declared like

void BindToText<T>(Control control, Expression<Func<T>> property)
{
    var mex = property.Body as MemberExpression;
    string name = mex.Member.Name;

    control.DataBindings.Add("Text", ??? , name);
}

so what do I put in ??? for the instance of my Foo class. How do I get a refernce to the calling foo instance from the lambda expression?

edit: The instance should be there somewhere because I can call property.Compile() and create a delegate that uses the foo instance inside my BindToText function. So my question is if this can be done without adding a reference to the instance in the function parameters. I call upon Occum's Razor to yield the simplest solution.

edit 2: What many have failed to notice is the closure that exists in accessing the instance of foo inside my function, if I compile the lambda. How come the compiler knows where to find the instance, and I don't? I insist that there has to be an answer, without having to pass an extra argument.


Solution

Thanks to VirtualBlackFox the solution is such:

void BindText<T>(TextBoxBase text, Expression<Func<T>> property)
{
    var mex = property.Body as MemberExpression;
    string name = mex.Member.Name;
    var fex = mex.Expression as MemberExpression;
    var cex = fex.Expression as ConstantExpression;            
    var fld = fex.Member as FieldInfo;
    var x = fld.GetValue(cex.Value);
    text.DataBindings.Add("Text", x, name);            
}

which alows me to simply type BindText(titleText, () => foo.Title);.

Accepted Answer

Small LINQPad sample of what you want :

void Foo<T>(Expression<Func<T>> prop)
{
    var propertyGetExpression = prop.Body as MemberExpression;

    // Display the property you are accessing, here "Height"
    propertyGetExpression.Member.Name.Dump();

    // "s" is replaced by a field access on a compiler-generated class from the closure
    var fieldOnClosureExpression = propertyGetExpression.Expression as MemberExpression;

    // Find the compiler-generated class
    var closureClassExpression = fieldOnClosureExpression.Expression as ConstantExpression;
    var closureClassInstance = closureClassExpression.Value;

    // Find the field value, in this case it's a reference to the "s" variable
    var closureFieldInfo = fieldOnClosureExpression.Member as FieldInfo;
    var closureFieldValue = closureFieldInfo.GetValue(closureClassInstance);

    closureFieldValue.Dump();

    // We know that the Expression is a property access so we get the PropertyInfo instance
    // And even access the value (yes compiling the expression would have been simpler :D)
    var propertyInfo = propertyGetExpression.Member as PropertyInfo;
    var propertyValue = propertyInfo.GetValue(closureFieldValue, null);
    propertyValue.Dump();
}

void Main()
{
    string s = "Hello world";
    Foo(() => s.Length);
}

Popular Answer

Don't. Just modify the method to take another parameter, as described in #3444294. For your example, it may be something like this:

void BindToText<T>(Control control, T dataSource, Expression<Func<T>> property)
{
    var mex = property.Body as MemberExpression;
    string name = mex.Member.Name;

    control.DataBindings.Add("Text", dataSource, name);
}

and would be called like

BindToText(titleTextBox, foo, ()=>foo.Title );

Still nice, but easy to understand. There's no magic happening. ;)




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why