2012-04-17 65 views
4

我在CodePlex上有人要求爲CodePlex上的項目比較HashSets:http://comparenetobjects.codeplex.com/。我需要能夠確定一個類型是否是一個哈希集合,然後得到枚舉器。我不知道該怎麼投它。以下是我有一個IList:如何確定類型是否爲HashSet類型以及如何投射它?

private bool IsIList(Type type) 
    { 
     return (typeof(IList).IsAssignableFrom(type)); 
    } 


    private void CompareIList(object object1, object object2, string breadCrumb) 
    { 
     IList ilist1 = object1 as IList; 
     IList ilist2 = object2 as IList; 

     if (ilist1 == null) //This should never happen, null check happens one level up 
      throw new ArgumentNullException("object1"); 

     if (ilist2 == null) //This should never happen, null check happens one level up 
      throw new ArgumentNullException("object2"); 

     try 
     { 
      _parents.Add(object1); 
      _parents.Add(object2); 

      //Objects must be the same length 
      if (ilist1.Count != ilist2.Count) 
      { 
       Differences.Add(string.Format("object1{0}.Count != object2{0}.Count ({1},{2})", breadCrumb, 
               ilist1.Count, ilist2.Count)); 

       if (Differences.Count >= MaxDifferences) 
        return; 
      } 

      IEnumerator enumerator1 = ilist1.GetEnumerator(); 
      IEnumerator enumerator2 = ilist2.GetEnumerator(); 
      int count = 0; 

      while (enumerator1.MoveNext() && enumerator2.MoveNext()) 
      { 
       string currentBreadCrumb = AddBreadCrumb(breadCrumb, string.Empty, string.Empty, count); 

       Compare(enumerator1.Current, enumerator2.Current, currentBreadCrumb); 

       if (Differences.Count >= MaxDifferences) 
        return; 

       count++; 
      } 
     } 
     finally 
     { 
      _parents.Remove(object1); 
      _parents.Remove(object2); 
     } 
    } 
+0

爲什麼不使用'ICollection'? – SLaks 2012-04-17 01:03:40

+0

這是行不通的: public bool IsHashSet(Type type) { \t return(typeof(ICollection).IsAssignableFrom(type)); } [測試] 公共無效IsHashSet() { \t 的HashSet的Hashset =新的HashSet (); \t類型type = hashSet.GetType(); \t Assert.IsTrue(_compare.IsHashSet(type)); } – 2012-04-17 01:12:36

回答

2

我相信這是足夠直接與ICollection<T>IEnumerable<T>通用接口,而不是HashSet<T>。您可以使用以下方法檢測這些類型:

// ... 
    Type t = typeof(HashSet<int>); 
    bool test1 = GenericClassifier.IsICollection(t); // true 
    bool test2 = GenericClassifier.IsIEnumerable(t); // true 
} 
// 
public static class GenericClassifier { 
    public static bool IsICollection(Type type) { 
     return Array.Exists(type.GetInterfaces(), IsGenericCollectionType); 
    } 
    public static bool IsIEnumerable(Type type) { 
     return Array.Exists(type.GetInterfaces(), IsGenericEnumerableType); 
    } 
    static bool IsGenericCollectionType(Type type) { 
     return type.IsGenericType && (typeof(ICollection<>) == type.GetGenericTypeDefinition()); 
    } 
    static bool IsGenericEnumerableType(Type type) { 
     return type.IsGenericType && (typeof(IEnumerable<>) == type.GetGenericTypeDefinition()); 
    } 
} 
1

您需要循環GetInterfaces(),並檢查其是否實現了一個接口,其中IsGenericType是真實的,GetGenericTypeDefinition() == typeof(ISet<>)

相關問題