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

一部のコレクションは両方のインターフェイスを実装しているため、コレクションを次のような具体的なインターフェイスに変換する必要があります

((ICollection)myList).IsNotEmpty();

または

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

そして、はい、obj == nullの場合はNullReferanceExceptionが発生するため、nullチェックを削除できます;)これは、拡張メソッドが、拡張メソッドなしで実行できる0とCountを比較することを意味します;)

于 2009-10-09T15:35:10.847 に答える
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));
}

は、Linq-to-EntityまたはLinq-to-SQLを使用している場合にデータベース要求をトリガーできるメソッドであるIEnumerable<T>ため、この機能を使用したくないことに注意してください。Count()

于 2009-10-12T10:43:23.147 に答える