c# - 如何判断一个类型是否为 HashSet 类型以及如何强制转换?

标签 c# .net

我在 GitHub 上有人要求能够比较我在 GitHub 上的项目的哈希集: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);
    }
}

最佳答案

我相信直接使用 ISet<T> 就足够了, ICollection<T>IEnumerable<T>通用接口(interface)而不是 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());
    }
}

关于c# - 如何判断一个类型是否为 HashSet 类型以及如何强制转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183738/

相关文章:

c# - 如何在 C# 中按下和移动鼠标?

.net - 如何保证 XPath 'OR' 查询的节点顺序

c# - VS2012EXPRESS Ado.Net SQLite 缺少 DLL 问题

c# mysql - 已经有一个与此连接关联的打开的数据读取器,必须先将其关闭

c# - 在不同的线程工作时刷新页面

.net - 为 wp7 应用程序运行测试

c# - 通过 gmail 使用 System.Net.Mail 发送电子邮件

c# - 如何用C#在智能卡上写入文件

C++判断硬盘剩余空间

.net - BlockingCollection(T) 性能