views:

41

answers:

0

Consider that I have to get the overall performance of X. X has Y elements and Y in turn has Z elements which inturn has some N elements. To get the performance of X I do this:

List<double> XQ = new List<double>();
foreach (Elem Y in X.Y){
   List<double> YQ = new List<double>();
   foreach (Elem Z in Y.Z){
      List<double> ZQ = new List<double>();
      foreach (Elem N in Z.N){
         ZQ.Add(GetPerformance(N));
      }
      YQ.Add(AVG(ZQ));
   }
   XQ.Add(AVG(YQ));
}

AVG of XQ list gives the performance of X. The performance can be calculated for either X or Y or for Z. X, Y and Z share the same base class. So depending on the item given the foreach loop has to be executed. Currently I have a switch case to determine each item (X or Y or Z) and the foreach loop is repeated in the code pertaining to the item (eg. If Y foreach starts from Y.Z). Is is possible to convert this whole code generic using reflection instead of having to repeat it in each switch case?

Thanks