I am having dictionary which store Funct. While storing Func ( testDir.Add(1, p => p.Id) )its fine. When i get the Func from dictionary i want the name of property (Id) which i am stored in Func. I had tried Retrieving Property name from lambda expression Get property name and type using lambda expression this links. It works in simple Func. But with dictionary i am getting member = null in GetMemberInfo.
public static MemberInfo GetMemberInfo<T, U>(Expression<Func<T, U>> expression)
{
var member = expression.Body as MemberExpression;
if (member != null)
return member.Member;
throw new ArgumentException("Expression is not a member access", "expression");
}
static void Main(string[] args)
{
Dictionary<int, Func<Soure.Employee, int>> testDir = new Dictionary<int, Func<Soure.Employee, int>>();
testDir.Add(1, p => p.Id);
var testDirValue = testDir[1];
Expression<Func<Soure.Employee, int>> expr1 = mc => testDirValue(mc);
MemberInfo member = Program.GetMemberInfo(expr1);
Console.WriteLine(member.Name);
}