4

只想对语法 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)”

这个问题有什么规范的解决方案吗?

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

4

2 回答 2

4

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.

Here is the whole code :

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

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.

于 2009-10-12T10:43:23.147 回答
4

这是因为一些集合实现了这两个接口,你应该像这样将集合转换为具体接口

((ICollection)myList).IsNotEmpty();

或者

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

是的,如果 obj == null 你会得到 NullReferanceException 所以你可以删除空检查;)这意味着你的扩展方法只是比较 Count 和 0 ,你可以在没有扩展方法的情况下做到这一点;)

于 2009-10-09T15:35:10.847 回答