CSharp examples for System.Reflection:Member
Find Member from LambdaExpression
using System.Reflection; using System.Linq.Expressions; using System;/*from w ww . j ava2 s .c om*/ public class Main{ //public static string FindPropertyName(LambdaExpression lambdaExpression) //{ // return FindProperty(lambdaExpression).Name; //} public static MemberInfo FindMember(LambdaExpression lambdaExpression) { Expression expressionToCheck = lambdaExpression; bool done = false; while (!done) { switch (expressionToCheck.NodeType) { case ExpressionType.Convert: expressionToCheck = ((UnaryExpression)expressionToCheck).Operand; break; case ExpressionType.Lambda: expressionToCheck = lambdaExpression.Body; break; case ExpressionType.MemberAccess: var propertyInfo = ((MemberExpression)expressionToCheck).Member; return propertyInfo; case ExpressionType.Call: return ((MethodCallExpression)expressionToCheck).Method; default: done = true; break; } } return null; } }