I have a Company class that has a composite object as a property that has company object as its property and so on for many levels. I need to get the name of the property at the deepest level with the least amount of code.
Here's a pseudocode for the case:
class Company
{
public Person Boss
{
public Document CV
{
public string Title;
}
}
}
I need to get a string result of "Company.Boss.CV.Title" with as less code as possible. The naive way of doing this would be to try nameof(Company.Boss.CV.Title)
but as we know this will result in just "Title".
I need to work with types not pure strings to allow the compiler to throw an error whenever someone changes the property names. Obviously something like:
string.Join('.', nameof(Company), nameof(Company.Boss), nameof(Company.Boss.CV), nameof(Company.Boss.CV.Title))
will work correctly 100% of the time but there is too much boilerplate code. Any suggestions are highly appreciated!