ICollection/ICollection< T>歧义问题 [英] ICollection / ICollection<T> ambiguity problem
问题描述
只是想对语法句法糖进行简单扩展:
Just want to make simple extension for syntactic sygar :
public static bool IsNotEmpty(this ICollection obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
public static bool IsNotEmpty<T>(this ICollection<T> obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
当我使用某些集合时,它可以完美工作,但是当我使用其他集合时,我可以得到
It works perfectly when I work with some collections, but when working with others I get
以下方法或属性: 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)' 和 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.Generic.ICollection)'
The call is ambiguous between the following methods or properties: 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)' and 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.Generic.ICollection)'
此问题是否有任何规范解决方案?
Is there any canonical solution to this problem ?
不,我不想在调用此方法之前执行强制转换;)
No, I don't want to perform a cast before calling this method ;)
推荐答案
解决歧义的最佳方法:为所有常见的非通用ICollection类定义一个重载. 这意味着自定义ICollection将不兼容,但是随着泛型成为规范,这没什么大不了的.
My best way to solve the ambiguity : define an overload for all common non-generic ICollection classes. That means custom ICollection won't be compatible, but it's no big deal as generics are becoming the norme.
这是整个代码:
/// <summary>
/// Check the given array is empty or not
/// </summary>
public static bool IsNotEmpty(this Array obj)
{
return ((obj != null)
&& (obj.Length > 0));
}
/// <summary>
/// Check the given ArrayList is empty or not
/// </summary>
public static bool IsNotEmpty(this ArrayList obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given BitArray is empty or not
/// </summary>
public static bool IsNotEmpty(this BitArray obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given CollectionBase is empty or not
/// </summary>
public static bool IsNotEmpty(this CollectionBase obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given DictionaryBase is empty or not
/// </summary>
public static bool IsNotEmpty(this DictionaryBase obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given Hashtable is empty or not
/// </summary>
public static bool IsNotEmpty(this Hashtable obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given Queue is empty or not
/// </summary>
public static bool IsNotEmpty(this Queue obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given ReadOnlyCollectionBase is empty or not
/// </summary>
public static bool IsNotEmpty(this ReadOnlyCollectionBase obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given SortedList is empty or not
/// </summary>
public static bool IsNotEmpty(this SortedList obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given Stack is empty or not
/// </summary>
public static bool IsNotEmpty(this Stack obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
/// <summary>
/// Check the given generic is empty or not
/// </summary>
public static bool IsNotEmpty<T>(this ICollection<T> obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
请注意,我不希望它在IEnumerable<T>
上运行,因为Count()
是一种在使用Linq-to-Entity或Linq-to-SQL时可以触发数据库请求的方法.
Note that I did not want it to work on IEnumerable<T>
, because Count()
is a method that can trigger a database request if you are working with Linq-to-Entity or Linq-to-SQL.
这篇关于ICollection/ICollection< T>歧义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!