Reputation: 3811
I hope Key/Value Type call Execute<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> enums)
,non Key/Value Type call Execute<T>(this IEnumerable<T> enums)
But Dictionary object will call Execute<T>(this IEnumerable<T> enums)
instead of Execute<TKey, TValue>(this ICollection<KeyValuePair<TKey, TValue>> enums)
Ex:
void Main(){
var keyValueTypeData = new[] {
new Dictionary<string, string> (){{"Name" , "ITWeiHan" }}
}.AsEnumerable();
keyValueTypeData.Execute(); //call Execute<T>
var nonKeyValueTypeData = new[] {new {Name ="ITWeiHan" }};
nonKeyValueTypeData.Execute(); //call Execute<T>
}
public static class Test
{
public static void Execute<TKey, TValue>(this IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>> enums){}
public static void Execute<T>(this IEnumerable<T> enums){}
}
Upvotes: 1
Views: 51
Reputation: 23975
An array of Dictionary
is not an IEnumerable
of KeyValuePair
(a single Dictionary
is - but that isn't what you have).
I suspect what you meant to do is:
using System.Collections.Generic;
namespace Test
{
public static class Test
{
public static void Execute<TKey, TValue>(this IEnumerable<IEnumerable<KeyValuePair<TKey, TValue>>> enums)
{
}
public static void Execute<T>(this IEnumerable<T> enums)
{
}
}
public class Program
{
public static void Main()
{
IEnumerable<IEnumerable<KeyValuePair<string, string>>> data = new[] {
new Dictionary<string, string> (){
{"Name" , "ITWeiHan" }
}
};
data.Execute();
}
}
}
Note that part of the solution is to be explicit about the type - to ensure that the compiler is 'encouraged' to choose the method I want it to select.
i.e. I use IEnumerable<IEnumerable<KeyValuePair<string, string>>>
rather than var
.
Upvotes: 3
Reputation: 109762
Try this:
static void Main()
{
var keyValueTypeData = new[] {
new Dictionary<string, string> (){{"Name" , "ITWeiHan" }}
};
keyValueTypeData.SelectMany(x => x.AsEnumerable()).Execute(); //call Execute<TKey, TValue>
var nonKeyValueTypeData = new[] { new { Name = "ITWeiHan" } };
nonKeyValueTypeData.Execute(); //call Execute<T>
}
Note the use of SelectMany()
and AsEnumerable()
.
Upvotes: 2