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

发布于 2024-07-15 04:29:36 字数 3713 浏览 4 评论 0 原文

我想比较不同版本的两个对象并在 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;
}

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

  • firstname< /code> - 约翰·玛丽
  • 姓氏 - 库珀·洛
  • 办公室号码 - 22222 44444
  • 手机号码 - 989898 089089
  • 家庭号码 - 4242 43535

officeNoPhoneNumber 类型的层次结构丢失了,这对我来说显示很重要。

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

I want to compare two objects of different versions and display their differences in UI.

First I call a method to know if there is any difference between the two objects

The method is:

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

If the above method returns true, I call the GetDifferences method to get the differences which is:

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

For each difference I create an object of type ObjectDifference and add it to the array. The highlighted portion is the one where I am stuck! If the object contains another complex object, My program does give me the differences but I dont know which type it belonged to

For example, I have two objects of type Name

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

class PhoneNumber
{
  string officeNo, MobileNo, HomeNo;
}

while comparing two objects the output I get is plain -

  • firstname - John Mary
  • LastName - cooper Lor
  • officeNo - 22222 44444
  • MobileNo - 989898 089089
  • HomeNo - 4242 43535

The Hierarchy that officeNo is of type PhoneNumber is lost, which is important for me to display.

How should I maintain this type of tree while creating differences? Hope I am able to make my problem understood.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-07-22 04:29:36

你想要做和展示的东西本质上是复杂的。 我过去曾这样做过(对于基于 diffgram/delta 的流程),甚至尝试以简单友好方式显示嵌套更改也很棘手。

如果它适合您的用户群,一种选择可能是将两个图表简单地序列化为 xml,并使用类似 XML 差异

What you are trying to do and display is inherently complex. I've done this in the past (for diffgram/delta-based processes), and even trying to display nested changes in a simple and friendly way is tricky.

If it fits your user-base, one option might be to simply serialize the two graphs as xml, and use something like xml diff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文