比较Same类的对象并将值存储在另一个类的List中 [英] Compare objects of Same class and store value in a List of another class

查看:79
本文介绍了比较Same类的对象并将值存储在另一个类的List中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较同一个类的两个不同对象,并将该值存储在另一个类的列表中。它适用于基本数据类型,但是当类包含列表的属性或另一个类的属性(可能包含另一个类或List的属性)时,我遇到了问题。

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).

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; }
}

推荐答案

我会这样做:

你提升你的班级 SomeCustomClass使用名为Equals的方法。

此方法等待作为要比较的类的参数。在此方法中,您必须检查给定对象是否具有正确的类型。现在你可以比较所有项目了。如果不匹配,你停止比较并返回False-Result。

如果所有属性匹配,则返回True-Result。
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.


I'建议阅读:建议的最佳做法是什么?使用IEqualityComparer< T>? [ ^ ]

最有价值的答案的作者建议实施: IEquatable< t> .Equals Method(T) [ ^ ]方法。



试试!
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!


这里有一些与之相关的链接 您的查询。希望这会有所帮助。



提前致谢。





< a href =http://broadcast.oreilly.com/2010/09/understanding-c-equality-iequa.html> http://broadcast.oreilly.com/2010/09/understanding-c-equality-iequa .html [ ^ ]



http://www.cyotek.com/blog/comparing-the-properties-of-two-objects-via-reflection [ ^ ]
Hi, here are some links below related to your query. Hope this will help.

Thanks in advance.


http://broadcast.oreilly.com/2010/09/understanding-c-equality-iequa.html[^]

http://www.cyotek.com/blog/comparing-the-properties-of-two-objects-via-reflection[^]


这篇关于比较Same类的对象并将值存储在另一个类的List中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆