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

前端 未结 4 1371
感动是毒
感动是毒 2020-12-13 10:05

I have a simple class with a property

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

I am trying to simplify data binding by calling a fun

4条回答
  •  爱一瞬间的悲伤
    2020-12-13 10:33

    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(Control control, T dataSource, Expression> 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. ;)

提交回复
热议问题