質問

異なるバージョンの2つのオブジェクトを比較し、それらの違いをUIで表示したい。

最初にメソッドを呼び出して、2つのオブジェクトに違いがあるかどうかを確認します

方法は次のとおりです。

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型の2つのオブジェクトがあります

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

class PhoneNumber
{
  string officeNo, MobileNo, HomeNo;
}

2つのオブジェクトを比較すると、出力は単純です-

  • firstname -ジョン・メアリー
  • -クーパーLor
  • officeNo -22222 44444
  • MobileNo -989898 089089
  • HomeNo -4242 43535

officeNo のタイプが PhoneNumber である階層は失われます。これは私にとって重要です。

このタイプのツリーを維持しながら、違いを作成するにはどうすればよいですか?私の問題を理解できることを願っています。

役に立ちましたか?

解決

やろうとしていることと表示することは本質的に複雑です。これは過去に(diffgram / deltaベースのプロセスで)行ってきましたが、ネストされた変更を簡単な友好的な方法で表示しようとするのも難しいです。

ユーザーベースに適合する場合、1つのオプションは、2つのグラフをxmlとしてシリアル化し、 xml diff

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top