I want to be able to call this method like this:
test.TestMethod<From, To>(to => to.Prop1);
and have IDE auto-complete properties from To. In TestMethod
, I want to pull the name of the property. In this case Prop1
. It seems like its kind of working, but I'm not sure about the Expression<Func<TTo, object>>
, especially the object part.
public class Test
{
public void TestMethod<TFrom, TTo>(Expression<Func<TTo, object>> p)
{
}
}
It does what its supposed to now if I recurse through p, I can get to the name, but it doesn't look right to me, especially since pulling the name of the prop looks like:
((MemberExpression)((UnaryExpression)(((LambdaExpression)p).Body)).Operand)).Member.Name
Any way to do this cleaner?