0

I am trying to get something similar to nameof(property) but including the path of the object it belongs to (and including higher level objects if they exist).

Lets say

public class ClassA(){
    public ClassB { get; set;}
    public ClassC { get; set;}
    public ClassD { get; set;}
}

public ClassB(){
    public string PropOfB { get; set; }
}

public ClassC(){
    public string PropOfC { get; set; }
}

public ClassD(){
    public string PropOfD { get; set; }
    public ClassC ClassC { get; set; }

}

I have a method that returns nameof(property), but if I run into (PropOfC), I need a little more to determine which PropOfC I am getting returned. I would like my result to be ClassA.ClassD.ClassC.PropOfC

I've looked at the answers to: Using reflection in C# to get properties of a nested object

Get Object value by string path

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection

but these are if you have the object instance on the highest level.

The things I've tried (I am not able to look at them atm) all return NameSpace.ClassC.PropOfC, and that doesn't help for this situation.

I am doing this because of the need to show where in the object the specific prop that needs to be reviewed is without having to hard code the responses.

I am stuck in the mud on this.

  • I don't think you'll find a solution to this. As you've seen, with reflection you can get information about the `type` itself, but not an instance's "parents". The problem with trying to get information about the "parents" is the "child" has no knowledge of the parent. It only exists in memory. The parent possesses a memory address for the child, but the child does not have the same for the parent (and indeed it could have many parents, i.e. many objects referring to the same object. – MikeH May 06 '20 at 17:56
  • One thought, which may be impractical: When you create the child, you pass a reference to the parent, then when you need you can navigate up your chain. However, this could create a situation where the classes are tightly coupled and that is generally undesireable. – MikeH May 06 '20 at 17:59
  • _"I am doing this because of the need to show where in the object the specific prop that needs to be reviewed is without having to hard code the responses."_ Can you elaborate on that? I am having hard time understanding what problem you are trying to solve. – Fildor May 06 '20 at 18:01
  • No there is no such built in framework method available .. to cut short your answer – Rahul May 06 '20 at 18:05
  • @Fildor My current set up is using a method to return information about the property. Using nameof, they know what property, but not which property (for several different sub-objects that have their own property of the same name). I'd like to use (someMethod) to get the entire path, rather than having to hard code passing of "ClassA.ClassD.ClassC.PropOfC". – JuniorExtreme May 06 '20 at 18:09
  • It is possible and you can check the answer here: https://stackoverflow.com/questions/34424787/reflection-to-get-parent-object/34424897 and here https://stackoverflow.com/questions/12418208/get-instance-of-containing-class – Piotr May 06 '20 at 18:10
  • 2
    @Rahul It's not a duplicate of that linked question. The OP is not looking for the fully qualified name. This question is a better candidate for the duplicate: https://stackoverflow.com/questions/12418208/get-instance-of-containing-class – MikeH May 06 '20 at 18:11
  • @MikeH Thanks for your explanation. I see how that can be an issue "(and indeed it could have many parents, i.e. many objects referring to the same object" but then it would need to be contextualize? Either way, I don't want to reference it in a node-like chain. I didn't see https://stackoverflow.com/questions/12418208/get-instance-of-containing-class when I was searching, but would agree that would be the question for dupe. Glad I still came out of this with some information. – JuniorExtreme May 06 '20 at 18:24
  • @JuniorExtreme you can try to write something like this using [Expression Trees](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/) (with some limitations, performance considerations and a little bit lost compile time safety), I've created [gist](https://gist.github.com/gurustron/a5bee2a55304731f96f4bdc3702a4f75) for your inspiration. – Guru Stron May 06 '20 at 19:37
  • @GuruStron I figured it would be possible with some reflection magic, but I was just given the "no go". The system performance would be the biggest issue for where this would be implemented. – JuniorExtreme May 07 '20 at 14:26
  • @JuniorExtreme you can always cache the execution results. – Guru Stron May 07 '20 at 15:13

0 Answers0