I have a common problem, I'm trying to get around a certain way.
Mostly with Winforms, I am trying to set the DisplayMember and ValueMember elements in a form. You usually install it like this:
this.testCombobox.DisplayMember = "PropertyOne"; this.testCombobox.ValueMember = "PropertyTwo";
I want to rewrite this as follows:
this.testCombobox.DisplayMember = ClassOne.GetPropertyName(c => c.PropertyOne); this.testCombobox.ValueMember = ClassOne.GetPropertyName(c => c.PropertyTwo);
(NOTE: 2 method calls must be static in order to save the creation of objects here)
All my classes that I'm trying to do inherit from the base class "BaseObject", so I added a method to it as follows:
public static string GetPropertyName<T, P>(Expression<Func<T, P>> action) where T : class { MemberExpression expression = action.Body as MemberExpression; return expression.Member.Name; }
However, to use this, I need to write the following code:
this.testCombobox.DisplayMember = BaseObject.GetPropertyName((ClassOne c) => c.PropertyOne);
My question is: how do I rewrite the BaseObject.GetPropertyName method to achieve what I want? I feel that I am very close, but I canβt think how to change it.
source share