2017-03-22 1 views
1

나는 Value에 List가있는 Dictionary의 확장 메서드를 정의하는 데 어려움을 겪고 있습니다.TValue에서 IList를 사용하여 IDictionary에 대한 확장 메서드를 만드는 방법은 무엇입니까?

public static bool MyExtensionMethod<TKey, TValue, K>(this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second) where TValue : IList<K> 
    { 
     //My code... 
    } 

내가이 클래스가이 기능을 사용하려면 :

나는이했던

public class A 
{ 
    public Dictionary<int, List<B>> MyPropertyA { get; set; } 
} 

public class B 
{ 
    public string MyPropertyB { get; set; } 
} 

을하지만 할 때 :

var a1 = new A(); 
var a2 = new A(); 
var a = a1.MyPropertyA.MyExtensionMethod(a2.MyPropertyA) 

나는이 오류가 '메서드'... '에 대한 형식 인수가 사용에서 유추 될 수 없음'

메소드를 정의하거나 호출해야하는 방법은 무엇입니까? 미리 감사드립니다 !! 일반적인 제약없이

답변

1

, 정의하는 것이 훨씬 쉽습니다 :

public static class Extensions 
{ 
    public static bool MyExtensionMethod<TKey, TValue>(
     this IDictionary<TKey, List<TValue>> first, 
     IDictionary<TKey, List<TValue>> second) 
    { 
     return true; 
    } 
} 

public class A 
{ 
    public Dictionary<int, List<B>> MyPropertyA { get; set; } 
} 
public class B 
{ 
    public string MyPropertyB { get; set; } 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 

     var a1 = new A(); 
     var a2 = new A(); 
     var a = a1.MyPropertyA.MyExtensionMethod(a2.MyPropertyA); 
    } 
} 

난 당신이 3 일반적인 인수 K을 필요로 확실하지 않다. 이 방법은 사용하기에 충분해야합니다.

참고로, 키와 목록이있는 사전의 일종 인 Lookup 클래스에 대해 알아야합니다. 단,이 키는 불변입니다.

public static class Extensions 
{ 
    public static bool MyExtensionMethod<TKey, TValue>(
     this ILookup<TKey, TValue> first, 
     ILookup<TKey, TValue> second) 
    { 
     return true; 
    } 
} 

public class A 
{ 
    public ILookup<int, B> MyPropertyA { get; set; } 
} 
public class B 
{ 
    public string MyPropertyB { get; set; } 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 

     var a1 = new A(); 
     var a2 = new A(); 
     var a = a1.MyPropertyA.MyExtensionMethod(a2.MyPropertyA); 
    } 
} 
+0

Lookup 클래스를 살펴 보겠습니다. 매우 감사합니다. – joacoleza

관련 문제