Pregunta

Quiero comparar dos objetos de diferentes versiones y mostrar sus diferencias en la interfaz de usuario.

Primero llamo a un método para saber si hay alguna diferencia entre los dos objetos

El método es:

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

Si el método anterior devuelve verdadero, llamo al método GetDifferences para obtener las diferencias, que es:

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

Para cada diferencia, creo un objeto de tipo ObjectDifference y lo agrego a la matriz. ¡La parte resaltada es aquella en la que estoy atrapado! Si el objeto contiene otro objeto complejo, Mi programa me da las diferencias, pero no sé a qué tipo pertenecía

Por ejemplo, tengo dos objetos de tipo Nombre

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

class PhoneNumber
{
  string officeNo, MobileNo, HomeNo;
}

al comparar dos objetos, el resultado que obtengo es simple -

  • nombre - John Mary
  • Apellido - Cooper Lor
  • officeNo - 22222 44444
  • MobileNo - 989898 089089
  • HomeNo - 4242 43535

La jerarquía que officeNo es del tipo PhoneNumber se pierde, lo cual es importante para mí mostrar.

¿Cómo debo mantener este tipo de árbol al crear diferencias? Espero poder hacer entender mi problema.

¿Fue útil?

Solución

Lo que intenta hacer y mostrar es intrínsecamente complejo. He hecho esto en el pasado (para procesos basados ??en diffgram / delta), e incluso intentar mostrar los cambios anidados de una manera simple y amigable es complicado.

Si se ajusta a su base de usuarios, una opción podría ser simplemente serializar los dos gráficos como xml y usar algo como xml diff .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top