I'm looping through various collections and if a particular error condition is met, then I need the full object graph, i.e. which index has the issue.
Sample Code:
foreach (var sale in allSales) {
foreach (var discount in sale.orders.detail.discounts) {
if (errorConditionMet) {
// print full object graph. For example, perhaps it's the second Sale (index one), but first discount object (index zero):
// We have "discount" object, but want to print:
// allSales[1].sale.orders.detail.discounts[0]
}
It's possible to just maintain counters (and is perhaps more performant):
string.Format("allSales[{0}].sale.orders.detail.discounts[{1}]", saleCount, discountCount);
// prints: allSales[1].sale.orders.detail.discounts[0]
but I'm wondering if this is possible with C# Reflection? I'll need this in multiple classes, so it would be great to pass an object to a method and return the object graph, totally dynamic:
var resultOne = GetViaReflection(discount);
// returns: allSales[1].sale.orders.detail.discounts[0]
var resultTwo = GetViaReflection(currentAnimal);
// returns: animals[3].animal.types[2]