c# - Implement Same Function for different Generic Types with Reflection -


i'm working on solution compare 2 elemts of same object eachother. code looks this:

public double compare(data o)     {         double same = 0;         double different = -1;          foreach (var prop in o.gettype().getproperties())         {             if (prop.getvalue(o) == prop.getvalue(this))                 same++;             else                 different++;         }                     return (same / (different + same)) * 100;     } 

data example implementation of idata interface, created own. since there more types of different datastructures, there identical implementations of function in each , every object, implements particular interface. now, kinda disturbes me because seems stupid have exact same lines of code in different classes.

is there chance, can use 1 method of different classes , still work reflection? thought szenario quite while , couldn't figure out how refere correct "this" reference. idea got is, write helper class 2 parameters function , call helper method in specific data call implementation. code this:

class comparehelper {      public double compare(data o, data callingobject)      {         double same = 0;         double different = -1;          foreach (var prop in o.gettype().getproperties())         {             if (prop.getvalue(o) == prop.getvalue(callingobject))                 same++;             else                 different++;         }           return (same / (different + same)) * 100;      }  } 

any other recommendations?

thanks in advance!

i don't know why figure out myself. i'm working abstract class, implements compare method (as stated above) , different types specializations of base class "data".


Comments