2009-10-09 4 views
4

그냥 syntactic sygar에 대한 간단한 확장을 만들고 싶어 : 나는 몇 가지 컬렉션을 작업 할 때은 ICollection/ICollection에 <T>이 모호한 문제

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.Is 는 NotEmpty (System.Collections.Generic.ICollection) '

이 문제에 대한 정규 솔루션이 있습니까?

가 아니,이 메소드를 호출하기 전에 캐스트를 수행하지 않으려는 )

일부 컬렉션은 두 인터페이스를 구현 때문입니다
+0

이 문제가있는 컬렉션을 인용 할 수 있습니까? 답변을 확인할 수 있습니까? –

+1

그 선언이 확실합니까? 오류 메시지는 ICollection보다는 IList임을 제안하는 것 같습니다. –

+0

IEnumerable, ICollection 및 IList의 제네릭 버전과 비 제네릭 버전의 확장 메서드를 사용하여이 문제를 해결했습니다. –

답변

3

모호성을 해결할 수있는 최선의 방법 : 모든 일반적인 제네릭 ICollection 클래스에 대한 과부하를 정의하십시오. 즉, 맞춤 ICollection은 호환되지 않지만 제네릭이 표준이되어 가고 있기 때문에 큰 문제는 아닙니다. 내가 Count() 당신이 Linq에 - 투 - 엔티티로 작업하는 경우 데이터베이스 요청을 트리거 할 수있는 방법이 있기 때문에, IEnumerable<T> 작업을하고 싶지 않았다

/// <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-SQL.

+2

그것을 사용하여 몇 주 후에, 그것은 가장 좋은 해결책입니다, 우리는 그것을 대량 채택했습니다. – Mose

4

, 당신은 같은 구체적인 인터페이스 모음을 변환해야이

((ICollection)myList).IsNotEmpty(); 

또는 확장 방법은 단지 비교 것을 의미)

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

그리고 OBJ == null이 당신이 NULL 체크를 제거 할 수 있도록하면 참으로 당신은 NullReferanceException을 얻을 것이다 확장 메소드없이 할 수있는 whith를 계산하십시오.)

+0

* 모든 제네릭 버전은 비 제너릭 버전을 구현하므로 제네릭 클래스는 둘 다 가질 수 있습니다. –

+0

예, 제네릭 버전 –

+1

에서이 오류가 발생할 수 있습니다. 제 질문의 마지막 문장에서 말씀 드렸듯이, 전화하기 전에는 캐스트를 수행하고 싶지 않습니다. SYNTACTIC SUGAR의 경우 캐스트를 추가해야하는 경우 쓸모가 없습니다. p 모든 질문에 대한 모호성을 해결할 수있는 방법은 무엇입니까? – Mose