Click here to Skip to main content
15,885,186 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to compare two different objects of same class and store the value in a list of an another class. It is working fine for basic data type but I am facing the problem when class contains property of a list or property of another class ( may contain property of another class or List).
C#
class Program
{
 static void Main()
 {
   SomeCustomClass a = new SomeCustomClass();
   SomeCustomClass b = new SomeCustomClass();
   a.CwNumber = "A";
   a.MaterialId = 1;
   a.MaterialName = "Material1";
   a.ListMyClass.Add( new MyClass1 { id = 11,Name="Name1" });
   a.ListMyClass.Add(new MyClass1 { id = 12, Name = "Name2" });

   b.CwNumber = "A";
   b.MaterialId = 2;
   b.MaterialName = "Material2";
   b.ListMyClass.Add(new MyClass1 { id = 21, Name = "Name3" });
   b.ListMyClass.Add(new MyClass1 { id = 22, Name = "Name4" });

   List<Variance> rt = a.GetDiffValue(b);
 }

public List<Variance> GetDiffValue<T>(this T val1, T val2)
{
  List<Variance> variances = new List<Variance>();
  Variance v = new Variance();
  PropertyInfo[] pi = val1.GetType().GetProperties();
  foreach (PropertyInfo p in pi)
   {
       Variance v1 = new Variance();
       if (p.PropertyType.IsGenericType == true)
       {
           v1.Prop = p.Name;
           v1.valA = p.GetValue(val1, null);
           v1.valB = p.GetValue(val2, null);
       }
       else
       {
           v1.Prop = p.Name;
           v1.valA = p.GetValue(val1,null);
           v1.valB = p.GetValue(val2,null);
       }
       if (!v1.valA.Equals(v1.valB))
       {
           variances.Add(v);
       }
   }
   return variances;
 }
}

public class SomeCustomClass
{
   public int MaterialId { get; set; }
   public string CwNumber { get; set; }
   public List<MyClass1> ListMyClass { get; set; }
   public UsesModel Uses { get; set; }

   public SomeCustomClass()
   {
       ListMyClass = new List<MyClass1>();
   }
}
public class MyClass1
{
   public int id;
   string Name;
}

public class Variance
{
    public string propertyName { get; set; }
    public object OldValue { get; set; }
    public object NewValue { get; set; }
}
Posted
Updated 20-Nov-15 1:04am
v3
Comments
Tomas Takac 20-Nov-15 8:29am    
I guess you have to inspect the objects recursively until you reach primitive types. And store the property path in the Variance class.
Anadi Chakraborty 20-Nov-15 12:17pm    
can you please explain with a sample code ??
Maciej Los 20-Nov-15 14:45pm    
A 5! even if a question is not well formed. On the first look, you want to compare both instances of SomeCustomClass and get an information about difference of each property. Am i right?
BillWoodruff 20-Nov-15 15:36pm    
Is it absolutely necessary that you write a Generic Method to analyze the differences ?

I would do it like this :
You enhance your class "SomeCustomClass" with a method called "Equals".
This method awaits as parameter the class to compare. In this method you have to check if the given object has the right Type. Now you could compare all items. In case of mismatch you stop comparing and return a "False"-Result.
If all properties matches you return a "True"-Result.
 
Share this answer
 
Comments
Anadi Chakraborty 20-Nov-15 12:18pm    
I have to log the difference..
Ralf Meier 23-Nov-15 8:32am    
Could you give more explaination about this.
What should be logged ?
The Name of the 1st different Property or it's content ?
Each Property (Name and/or content) ?
I'd suggest to read this: What's the recommended best practice for using IEqualityComparer<T>?[^]
An author of the most valuable answer recommends to implement: IEquatable<t>.Equals Method (T)[^] method.

Try!
 
Share this answer
 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900