-1

Let me show you some code right away:

class MyClass {

    public MyClass(Customer c) {
        ExtractRoot(() => c.FirstName);
    }
}

ExtractRoot is accepting a Expression<Func<object>> as its parameter.

I am trying to extract the customer object 'c' from the expression tree. I am passing it to the ExtractRoot method, and I know how to get the ConstantExpression which represents the customer object. But, when I call ToString() on that object I get this printed:
MyAssemblyName.CustomNamespace.MyClass+<>c__DisplayClass2

Can someone please explain what this means and why I am not getting my customer's ToString() method called, it is as if the object I am getting is not the customer, why and what am I getting back?

Finally, how can I get the actual customer object, or the root of my expression?

Bob2Chiv
  • 1,858
  • 2
  • 19
  • 29
Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • I'm not sure I understand the question, do you mean `constantExpression.Value`? – Joachim Isaksson Jul 28 '13 at 19:18
  • 1
    () => c.FirstName is a typeless delegate. To get to the root you need a delegate in the form of c => c.FirstName, i think – Rob van der Veer Jul 28 '13 at 19:19
  • @JoachimIsaksson correct, i keep descending the tree until i arrive at the `ConstantExpression` then i get its value like you said but i get what i showed you ? – Ibrahim Najjar Jul 28 '13 at 19:20
  • possible duplicate of [What does "DisplayClass" name mean when calling lambda?](http://stackoverflow.com/questions/16401860/what-does-displayclass-name-mean-when-calling-lambda) – Erik Schierboom Jul 28 '13 at 19:35
  • Why the down vote !! and this isn't a complete duplicate @ErikSchierboom because the related answer covers part of my question but what about getting the actual root object. – Ibrahim Najjar Jul 28 '13 at 21:52
  • @RobvanderVeer it turned out that you can but you need some reflection, i found the answer in another stackoverflow question and put the link in my answer. Thank You. – Ibrahim Najjar Jul 29 '13 at 11:48

1 Answers1

0

I have found the complete solution to my answer which has two parts:

  1. Why the DisplayClass and what it means can be found in the answer to this question: What does “DisplayClass” name mean when calling lambda?
  2. How to get the actual root of the expression (which is possible by the way) can be found in this questions answer: Getting the object out of a MemberExpression.
Community
  • 1
  • 1
Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95