我在GitHub上有人要求能够在GitHub上为我的项目比较HashSets: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);
}
}
答案 0 :(得分:3)
我认为直接使用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());
}
}
答案 1 :(得分:1)
您需要遍历GetInterfaces()
并检查它是否实现了IsGenericType
为真且GetGenericTypeDefinition() == typeof(ISet<>)
答案 2 :(得分:0)
这是内置于HashSets ...使用方法SymmetricExceptWith。还有其他内置的比较。请参阅:http://msdn.microsoft.com/en-us/library/bb336848.aspx
答案 3 :(得分:0)
接受的答案不区分Dictionary
类型以及可能ICollection
和IEnumerable
的其他子类。这样做效果更好:
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