2011-03-24 3 views
0

일반화 된 방식으로 두 객체를 비교하는 비교 자 클래스를 만들고 싶습니다. 필드, 속성, 객체, 두 객체 내의 객체 목록을 검사합니다. 두 객체를 public 속성 및 필드와 비교하는 코드를 만들었습니다. 그러나 주어진 입력 개체 내에서 개체 (및 개체 목록)를 비교하여 추가하려고합니다.리플렉션을 사용하여 비교 자 클래스를 만드는 방법은 무엇입니까?

public static class Comparer<T> 
    { 
     /// <summary> 
     /// Comparer method formcomparing two objects 
     /// </summary> 
     /// <param name="x">Object x of type T</param> 
     /// <param name="y">Object y of type T</param> 
     /// <returns>int value 0/1</returns> 
     public static int Compare(T x, T y) 
     { 
      Type type = typeof(T); 

      //Collecting public properties and fields 
      PropertyInfo[] properties = type.GetProperties(); 
      FieldInfo[] fields = type.GetFields(); 

      int compareValue = 0; 

      // Loop for comparing one by one properties values of two objects. 
      foreach (PropertyInfo property in properties) 
      { 
       IComparable valx = property.GetValue(x, null) as IComparable; 
       if (valx == null) 
        continue; 
       object valy = property.GetValue(y, null); 

       compareValue = valx.CompareTo(valy); 

       if (compareValue != 0) 
        return compareValue; 
      } 


      // Loop for comparing one by one fields values of two objects. 
      foreach (FieldInfo field in fields) 
      { 
       IComparable valx = field.GetValue(x) as IComparable; 
       if (valx == null) 
        continue; 
       object valy = field.GetValue(y); 

       compareValue = valx.CompareTo(valy); 
       if (compareValue != 0) 
        return compareValue; 
      } 

      return compareValue; 
     } 

     /// <summary> 
     /// Comparer method for comparing to list objects 
     /// </summary> 
     /// <param name="x">List object of T type</param> 
     /// <param name="y">List object of T type</param> 
     /// <returns>Result of comparision as true/false</returns> 
     public static bool Compare(List<T> x, List<T> y) 
     { 
      // Checking input lists as a null. 
      if (x == null || y == null) 
      { 
       return false; 
      } 

      // Checking input lists count is equal or not. 
      if (x.Count() != y.Count()) 
      { 
       return false; 
      } 

      // Loop that invoke compare method for each of list objects. 
      for (int iCntr = 0; iCntr < x.Count(); iCntr++) 
      { 
       int result = Compare(x[iCntr], y[iCntr]); 
       if (result != 0) 
       { 
        return false; 
       } 
      } 
      return true; 
     } 
} 

문제를 해결할 생각이 있습니까 ?? 당신이 그것에 대해 어떤 심판을 찾으면 나에게 답장 해주세요.

안부, 있는 Girish

답변

0

당신은 발견 된 모든 속성에 대해 재귀를 사용할 필요가있다.

Pseudocode 
public static int Compare(object x, object y) 
{ 
    ... handle if one or both are null 
    ... handle if both same type 
    if (IsArray(x)) 
    { 
     ?? equal also means same order?? 
     for i=0 to min(x.lenght, y.length)-1 { 
      int subcompareresult = Compare(x[i], y[i]) 
      if subcompareresult != 0 
       return subcompareresult // difference found 
     } 

     // elements compared so far are same 
     ... handle different length 
    } else if IsClass(x) 
       foreach subproperty in x.Properties 
     int subcompareresult = Compare(x[subproperty ], y[subproperty ]) 
     if subcompareresult != 0 
      return subcompareresult // difference found 
      else 
     ... handle value compare 

array와 class 내의 하위 항목을 비교하는 재귀 호출에 유의하십시오.

SharpSerializer 소스 코드를 살펴볼 수 있습니다. SharpSerializer는 XML 또는 바이너리 형식으로 개체 그래프를 serialize합니다. 재귀 적 탐색을 사용하는 부분은 SharpSerializer/.../PropertyFactory.cs

에서 찾을 수 있습니다.
관련 문제