1

Is there a way to create a Binding from a Expression rather than having to hardcode the name of the Property?

Normally you do:

new Binding("MyPropertyName")

Is there a way, part of the Framework, that you could do instead, something like:

new Binding(x=> x.MyPropertyName)

Does the Framework offer this feature?

2
  • I don't think the type inference system would be able to determine what type x is in your example. You would need something like new Binding(MyThing x=> x.MyPropertyName) for it to work at all. I don't think this directly exists, but you could easily create a helper function to do it. Commented May 6, 2015 at 13:47
  • @BradleyUffner - Yes, I just wrote a lose example to get my point across, I realize the x is not enough information, thanks for pointing it out though. Commented May 6, 2015 at 14:00

1 Answer 1

1

At this moment is not possible. You have to wait for C# 6.0 and nameof operator - https://msdn.microsoft.com/en-us/magazine/dn802602.aspx

In other way you can use this simply method:

public static string GetPropertyName<T>(Expression<Func<T>> expression)
{
    MemberExpression body = (MemberExpression)expression.Body;
    return body.Member.Name;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.