문제

다른 버전의 두 객체를 비교하고 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의 객체를 생성하여 배열에 추가합니다. 강조 표시된 부분은 내가 붙어있는 곳입니다! 객체에 또 다른 복잡한 객체가 포함되어 있으면 내 프로그램은 차이점을 제공하지만 어떤 유형에 속한 지 모르겠습니다.

예를 들어, 나는 유형 이름의 두 객체가 있습니다.

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

class PhoneNumber
{
  string officeNo, MobileNo, HomeNo;
}

두 개의 객체를 비교하는 동안 내가 얻는 출력은 평범합니다.

  • firstname - 존 메리
  • LastName - 쿠퍼 로어
  • officeNo - 22222 44444
  • MobileNo - 989898 089089
  • HomeNo - 4242 43535

계층 officeNo 유형입니다 PhoneNumber 길을 잃어서 표시하는 것이 중요합니다.

차이를 생성 하면서이 유형의 트리를 어떻게 유지해야합니까? 내 문제를 이해할 수 있기를 바랍니다.

도움이 되었습니까?

해결책

당신이하려고하는 것은 본질적으로 복잡합니다. 나는 과거에 (diffgram/delta 기반 프로세스를 위해) 이것을했고 심지어 표시하다 단순한 중첩 변경 그리고 친절합니다 길은 까다 롭습니다.

사용자 기반에 맞는 경우 두 개의 그래프를 XML로 직렬화하고 다음과 같은 것을 사용하는 것입니다. XML Diff.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top