c# - Compare two objects and find the differences -


this question has answer here:

what best way compare 2 objects , find differences?

customer = new customer(); customer b = new customer(); 

one flexible solution: use reflection enumerate through of properties , determine , not equal, return list of properties , both differing values.

here's example of code start asking. looks @ field values right now, add number of other components check through reflection. it's implemented using extension method of objects use it.

to use

    somecustomclass = new somecustomclass();     somecustomclass b = new somecustomclass();     a.x = 100;     list<variance> rt = a.detailedcompare(b); 

my sample class compare against

    class somecustomclass     {         public int x = 12;         public int y = 13;     } 

and meat , potatoes

static class extentions {     public static list<variance> detailedcompare<t>(this t val1, t val2)     {         list<variance> variances = new list<variance>();         fieldinfo[] fi = val1.gettype().getfields();         foreach (fieldinfo f in fi)         {             variance v = new variance();             v.prop = f.name;             v.vala = f.getvalue(val1);             v.valb = f.getvalue(val2);             if (!v.vala.equals(v.valb))                 variances.add(v);          }         return variances;     }   } class variance {     public string prop { get; set; }     public object vala { get; set; }     public object valb { get; set; } } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -