5

我在 GitHub 上有人要求能够比较我在 GitHub 上的项目的 HashSet:https ://github.com/GregFinzer/Compare-Net-Objects/ 。我需要能够确定一个类型是否是哈希集,然后获取枚举器。我不知道要把它投射到什么地方。这是我对 IList 的要求:

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

4 回答 4

4

我相信直接使用ISet<T>ICollection<T>IEnumerable<T>通用接口而不是HashSet<T>. 您可以使用以下方法检测这些类型:

// ...
    Type t = typeof(HashSet<int>);
    bool test1 = GenericClassifier.IsICollection(t); // true
    bool test2 = GenericClassifier.IsIEnumerable(t); // true
    bool test3 = GenericClassifier.IsISet(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);
    }
    public static bool IsISet(Type type) {
        return Array.Exists(type.GetInterfaces(), IsGenericSetType);
    }
    static bool IsGenericCollectionType(Type type) {
        return type.IsGenericType && (typeof(ICollection<>) == type.GetGenericTypeDefinition());
    }
    static bool IsGenericEnumerableType(Type type) {
        return type.IsGenericType && (typeof(IEnumerable<>) == type.GetGenericTypeDefinition());
    }
    static bool IsGenericSetType(Type type) {
        return type.IsGenericType && (typeof(ISet<>) == type.GetGenericTypeDefinition());
    }
}
于 2012-04-17T05:00:10.033 回答
1

您需要循环GetInterfaces()并检查它是否实现了一个IsGenericType真实的接口并且GetGenericTypeDefinition() == typeof(ISet<>)

于 2012-04-17T01:14:33.427 回答
0

这是内置于 HashSets... 使用 SymmetricExceptWith 方法。还有其他内置的比较。请参阅:http: //msdn.microsoft.com/en-us/library/bb336848.aspx

于 2012-04-17T01:14:48.623 回答
0

接受的答案不区分Dictionary类型和可能的其他子类ICollectionIEnumerable。这效果更好:

Type t1 = typeof(HashSet<int>);
bool test1 = t1.IsGenericType && 
    t1.GetGenericTypeDefinition() == typeof(HashSet<>); // true

Type t2 = typeof(Dictionary<int, string>);
bool test2 = t2.IsGenericType && 
    t2.GetGenericTypeDefinition() == typeof(HashSet<>); // false

Type t3 = typeof(int);
bool test3 = t3.IsGenericType && 
    t3.GetGenericTypeDefinition() == typeof(HashSet<>); // false
于 2018-05-08T14:28:52.940 回答