mardi 1 décembre 2015

Get full object graph string C#

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?





Aucun commentaire:

Enregistrer un commentaire