Tôi muốn so sánh hai đối tượng của các phiên bản khác nhau và hiển thị sự khác biệt của chúng trong giao diện người dùng.Cách phân biệt hai phiên bản của cùng một đối tượng?
Trước tiên tôi gọi một phương thức để biết nếu có bất kỳ sự khác biệt giữa hai đối tượng
Phương pháp này là:
public bool AreEqual(object object1,object object2, Type comparisionType)
Nếu các phương pháp trên trả về true, tôi gọi phương thức GetDifferences
để có được khác biệt là:
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;
}
}
Đối với mỗi khác biệt, tôi tạo đối tượng kiểu ObjectDifference và thêm đối tượng vào mảng. Phần được đánh dấu là phần tôi bị kẹt! Nếu đối tượng chứa một đối tượng phức tạp, chương trình của tôi không cho tôi sự khác biệt nhưng tôi không biết loại nó thuộc về
Ví dụ, tôi có hai đối tượng thuộc loại Tên
class Name
{
string firstName, LastName;
List phNumber;
}
class PhoneNumber
{
string officeNo, MobileNo, HomeNo;
}
khi so sánh hai đối tượng người đầu ra tôi nhận được là đồng bằng -
firstname
- John MaryLastName
- cooper LorofficeNo
- 22222 44444MobileNo
- 989898 089089HomeNo
- 4242 43535
Các Hierarchy rằng officeNo
là loại PhoneNumber
bị mất, đó là điều quan trọng đối với tôi để hiển thị.
Tôi nên duy trì loại cây này trong khi tạo sự khác biệt như thế nào? Hy vọng tôi có thể làm cho vấn đề của tôi được hiểu.