How can I adapt the method below to work when the lambda expression refers to the actual instance itself?
e.g. instead of
x => x.Name
the expression is
x => x
so if I had some class "Car" I could return the string "Car" instead of only being able to operate on its properties (e.g. Car.Colour)
The method:
public static string GetMemberName(Expression expression)
{
if (expression is LambdaExpression)
expression = ((LambdaExpression)expression).Body;
if (expression is MemberExpression)
{
var memberExpression = (MemberExpression)expression;
if (memberExpression.Expression.NodeType ==
ExpressionType.MemberAccess)
{
return GetMemberName(memberExpression.Expression)
+ "."
+ memberExpression.Member.Name;
}
return memberExpression.Member.Name;
}
if (expression is UnaryExpression)
{
var unaryExpression = (UnaryExpression)expression;
if (unaryExpression.NodeType != ExpressionType.Convert)
throw new Exception(string.Format(
"Cannot interpret member from {0}",
expression));
return GetMemberName(unaryExpression.Operand);
}
throw new Exception(string.Format(
"Could not determine member from {0}",
expression));
}
i.e. I want something like:
if (expression is SomeExpressionThatReturnsAnInstance)
{
return (name of type of instance);
}
objectinstead ofproperty, is it ?x.ToString()orx.GetType().Name?