0

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();
}
Community
  • 1
  • 1
Victor Castro
  • 1,232
  • 21
  • 40

2 Answers2

1

I throw this in as the first intent: (not 100% done, still thinking about it, at least it handles your sample)

public static class MemberInfoGetting {
    public static string GetMemberName<T>(Expression<Func<T>> memberExpression) {
        MemberExpression expressionBody = (MemberExpression)memberExpression.Body;

        var str = expressionBody.ToString();
        var lst =  str.Split('.').Skip(2).ToList(); //This needs LINQ, otherwise do it manually

        StringBuilder retVal = new StringBuilder();

        for (int i = 0; i < lst.Count; i++) {
            retVal.Append(lst[i]);
            if(i != lst.Count -1) {
                retVal.Append(".");
            }
        }               

        return retVal.ToString();
    }
}
gregkalapos
  • 3,529
  • 2
  • 19
  • 35
  • Is it the correct solution to problem? I didn't get the class names. It does handle the sample because instance name used is same as class name and no inheritance is used in example. – Gaurav Gupta Nov 14 '16 at 11:05
  • First of all, i'm not sure that this is correct for every input. What was your input? (Show us the caller part..). Btw if I understand the problem correctly then we don't want the classname! we want the variable name! Btw. you can get the class name via (MemberExpression)memberExpression (but again, that's not what we want) – gregkalapos Nov 14 '16 at 11:07
  • try it using Test obj = new Test(); GetMemberName(() => obj.child); – Gaurav Gupta Nov 14 '16 at 11:09
  • Yes, that returns "obj.child", and that is what we want. Or did I get the problem statement wrong? – gregkalapos Nov 14 '16 at 11:10
  • i believe prob is to get class name and parent class if present. " I only get child and I have no idea how to get all the parents." – Gaurav Gupta Nov 14 '16 at 11:11
  • @LeCintas? Can you give us some input? – gregkalapos Nov 14 '16 at 11:14
0

Then maybe it is useful to have something like:

public class Test
{
    public string value = "Value";

    public Test child;

    public Test(Test childObject)
    {
        this.child = childObject;
    }
}

You can use this object for recursion then. As now you have nested objects. Something like this:

Test child = new Test(null);
Test parent = new Test(child);

So, if you create a method which takes as parameter the parent object in this case, you can get the parent and the child values as well. In this case, the exit condition of your recursion method being the Test object that has a null child.