簡體   English   中英

ICollection / ICollection <T>模糊問題

[英]ICollection / ICollection<T> ambiguity problem

只想對語法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)'

這個問題有任何規范的解決方案嗎?

不,我不想在調用此方法之前執行強制轉換;)

這是因為有些集合實現了兩個接口,你應該將集合轉換為這樣的具體接口

((ICollection)myList).IsNotEmpty();

要么

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

如果obj == null你會得到NullReferanceException,所以你可以刪除null check;)這意味着你的擴展方法只是比較Count whith 0,你可以在沒有擴展方法的情況下做;)

解決歧義的最佳方法:為所有常見的非泛型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> ,因為如果您正在使用Linq-to-Entity或Linq-to-SQL,則Count()是一種可以觸發數據庫請求的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM