ICollection / ICollection <t>歧义问题</t>

时间:2009-10-09 15:24:35

标签: c# extension-methods icollection ambiguous

只想为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));
}

当我使用某些收藏品时,它的效果非常好,但与其他人合作时我得到了

  

电话之间的暧昧不明确   以下方法或属性:   'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)'   和   'PowerOn.ExtensionsBasic.IsNotEmpty(了System.Collections.Generic.ICollection)'

此问题是否有规范解决方案?

不,我不想在调用此方法之前执行强制转换;)

2 个答案:

答案 0 :(得分:4)

这是因为某些集合实现了两个接口,您应该将集合转换为具体的接口,如此

((ICollection)myList).IsNotEmpty();

或者

((ICollection<int>)myIntList).IsNotEmpty();

是的,如果obj == null你会得到NullReferanceException,所以你可以删除null check;)这意味着你的扩展方法只是比较Count whith 0,你可以不用扩展方法;)

答案 1 :(得分:4)

解决歧义的最佳方法:为所有常见的非泛型ICollection类定义重载。 这意味着自定义ICollection将不兼容,但随着泛型成为规范,它并没有什么大不了的。

以下是整个代码:

/// <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时触发数据库请求的方法