c# - 如何区分同一对象的两个版本?

标签 c# comparison object diff hierarchy

我想比较两个不同版本的对象并在 UI 中显示它们的差异。

首先我调用一个方法来知道两个对象之间是否有任何区别

方法是:

public bool AreEqual(object object1,object object2, Type comparisionType)

如果上述方法返回 true,我将调用 GetDifferences 方法来获取差异,即:

public ObjectDifference[] GetObjectDifferences(object object1, object object2, Type comparisionType)
{
  ArrayList memberList = new ArrayList();
  ArrayList differences = new ArrayList();

  memberList.AddRange(comparisionType.GetProperties());
  memberList.AddRange(comparisionType.GetFields());

  for (int loopCount = 0; loopCount < memberList.Count; loopCount++)
  {
    object objVal1 = null;
    object objVal2 = null;
    MemberInfo member = ((MemberInfo)memberList[loopCount]);
    switch (((MemberInfo)memberList[loopCount]).MemberType)
    {
      case MemberTypes.Field:
        objVal1 = object1 != null ? ((FieldInfo)memberList[loopCount]).GetValue(object1) : null;
        objVal2 = object2 != null ? ((FieldInfo)memberList[loopCount]).GetValue(object2) : null;
        break;
      case MemberTypes.Property:

        objVal1 = object1 != null ? ((PropertyInfo)memberList[loopCount]).GetValue(object1, null) : null;
        objVal2 = object2 != null ? ((PropertyInfo)memberList[loopCount]).GetValue(object2, null) : null;
        break;
      default:
        break;
    }

    if (AreValuesDifferentForNull(objVal1, objVal2))
    {
      ObjectDifference obj = new ObjectDifference(objVal1, objVal2, member, member.Name);
      differences.Add(obj);
    }
    else if (AreValuesDifferentForPrimitives(objVal1, objVal2))
    {
      ObjectDifference obj = new ObjectDifference(objVal1, objVal2, member, member.Name);
      differences.Add(obj);
    }
    else if (AreValuesDifferentForList(objVal1, objVal2))
    {
      ObjectDifference[] listDifference = GetListDifferences((ICollection)objVal1, (ICollection)objVal2, member);
      differences.AddRange(listDifference);
    }
    else if ((!AreValuesEqual(objVal1, objVal2)) && (objVal1 != null || objVal2 != null))
    {
      ObjectDifference obj = new ObjectDifference(objVal1, objVal2, member, member.Name);
      differences.Add(obj);
    }
  }
  return (ObjectDifference[])differences.ToArray(typeof(ObjectDifference));
}


public class ObjectDifference
{
  private readonly object objectValue1;
  private readonly object objectValue2;
  private readonly System.Reflection.MemberInfo member;
  private readonly string description;

  public object ObjectValue1
  {
    get { return objectValue1; }
  }
  public object ObjectValue2
  {
    get { return objectValue2; }
  }
  public System.Reflection.MemberInfo Member
  {
    get { return member; }
  }
  public string Description
  {
    get { return description; }
  }

  public ObjectDifference(object objVal1, object objVal2, System.Reflection.MemberInfo member, string description)
  {
    this.objectValue1 = objVal1;
    this.objectValue2 = objVal2;
    this.member = member;
    this.description = description;
  }
}

对于每个差异,我创建一个 ObjectDifference 类型的对象并将其添加到数组中。突出显示的部分是我被卡住的部分!如果对象包含另一个复杂对象,我的程序确实给我差异但我不知道它属于哪种类型

比如我有两个Name类型的对象

class Name
{
  string firstName, LastName;
  List phNumber;
}

class PhoneNumber
{
  string officeNo, MobileNo, HomeNo;
}

比较两个对象时,我得到的输出很简单 -

  • 名字 - 约翰·玛丽
  • LastName - cooper Lor
  • officeNo - 22222 44444
  • 手机号 - 989898 089089
  • 家号 - 4242 43535

officeNo 属于 PhoneNumber 类型的层次结构丢失了,这对我来说很重要,需要显示。

我应该如何在创建差异的同时维护这种类型的树?希望我能够让我的问题得到理解。

最佳答案

您尝试执行和显示的内容本质上是复杂的。我过去曾这样做过(针对基于 diffgram/delta 的进程),甚至尝试以一种简单和友好的方式显示嵌套更改也很棘手。

如果适合您的用户群,一种选择可能是简单地将两个图序列化为 xml,并使用类似 xml diff 的东西.

关于c# - 如何区分同一对象的两个版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/641876/

相关文章:

C# Unity IEndDragHandler OnEndDrag 并不总是被调用

java - 深度反射(reflection)比较等于

asp.net - 如何将对象序列化为查询字符串格式?

c++ - 运算符重载添加两个对象错误

bash - 测试编译代码以返回预期输出/错误的最佳方法

Python 全局未在新对象实例中更新

c# - 不可为 Null 的字符串初始化为 Null

c# - Microsoft Graph - 允许用户访问应用程序/服务主体

c# - 打破一个单一的项目

arrays - 快速比较数组