Question

Je souhaite comparer deux objets de versions différentes et afficher leurs différences dans l'interface utilisateur.

J'appelle d'abord une méthode pour savoir s'il existe une différence entre les deux objets

La méthode est la suivante:

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

Si la méthode ci-dessus retourne la valeur true, j'appelle la méthode GetDifferences pour obtenir les différences, à savoir:

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

Pour chaque différence, je crée un objet de type ObjectDifference et l'ajoute au tableau. La partie en surbrillance est celle où je suis coincé! Si l’objet contient un autre objet complexe, mon programme me donne les différences mais je ne sais pas à quel type il appartenait

Par exemple, j'ai deux objets de type Nom

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

class PhoneNumber
{
  string officeNo, MobileNo, HomeNo;
}

lors de la comparaison de deux objets, le résultat obtenu est clair -

  • prénom - John Mary
  • LastName - cooper Lor
  • officeNo - 22222 44444
  • MobileNo - 989898 089089
  • AccueilNon - 4242 43535

La hiérarchie selon laquelle officeNo est de type PhoneNumber est perdue, ce qui est important pour moi pour l'affichage.

Comment dois-je entretenir ce type d’arbre tout en créant des différences? J'espère pouvoir faire comprendre mon problème.

Était-ce utile?

La solution

Ce que vous essayez de faire et d’afficher est intrinsèquement complexe. Je l’ai déjà fait par le passé (pour les processus basés sur diffgram / delta) et même essayer d’afficher les modifications imbriquées de manière simple et conviviale est délicat.

Si cela convient à votre base d'utilisateurs, vous pouvez éventuellement simplement sérialiser les deux graphiques en tant que xml et utiliser un type de type diff xml .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top