I try to get recursively the name of a variable, for example for the variable test.child
, if I follow this topic, I only get child
and I have no idea how to get all the parents.
Here is my test code:
public static class MemberInfoGetting
{
public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
{
MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
return expressionBody.Member.Name;
}
}
public class Test
{
public string child = "childValue";
}
static void Main(string[] args)
{
//string testVariable = "value";
Test test = new Test();
test.child = "newValue";
string nameOfTestVariable = MemberInfoGetting.GetMemberName(() => test.child);
Console.WriteLine(nameOfTestVariable + " | " + test.child);
Console.Read();
}