I have a function:
private string GetPropertyName(Expression<Func<object, object>> f) {
if ((f.Body as MemberExpression) != null) {
return (f.Body as MemberExpression).Member.Name;
}
return "";
}
And it is used this way:
string x1 = GetPropertyName(x => Property1);
string x2 = GetPropertyName(x => Property2);
string x3 = GetPropertyName(x => Property3);
where Property1 is an int, Property2 is a string, and Property3 is an object...
Only the names of Property2 and Property3 of types string and object respectfully are correctly returned, but Property1's f.Body as MemberExpression is null...
Why is this so, and how can we change the code so the function returns the Properties' names correctly?