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

前端 未结 4 1367
感动是毒
感动是毒 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:14

    Well, this is similar in tone to Hangy's solution, but is I think rather comfortable to use and does not require much magic:

    public static Binding CreateTextBinding(this T source, Expression> access)
    {
        var mex = access.Body as MemberExpression;
        string name = mex.Member.Name;
        return new Binding("Text", source, name);
    }
    

    This is basically an extension method that can be called on any object acting as source. It gives you back a Binding for a Text property which you can add to any Bindings collection.

提交回复
热议问题