Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#重载IEnumerable键/值类型和非键/值_C# - Fatal编程技术网

C#重载IEnumerable键/值类型和非键/值

C#重载IEnumerable键/值类型和非键/值,c#,C#,我希望键/值类型调用Execute(此IEnumerable枚举),非键/值类型调用Execute(此IEnumerable枚举) 但是Dictionary对象将调用Execute(此IEnumerable枚举)而不是Execute(此ICollection枚举) 例: void Main(){ var keyValueTypeData=new[]{ 新词典(){{“Name”,“ITWeiHan”} }.AsEnumerable(); keyValueTypeData.Execute();//

我希望键/值类型调用
Execute(此IEnumerable枚举)
,非键/值类型调用
Execute(此IEnumerable枚举)

但是Dictionary对象将调用
Execute(此IEnumerable枚举)
而不是
Execute(此ICollection枚举)

例:

void Main(){
var keyValueTypeData=new[]{
新词典(){{“Name”,“ITWeiHan”}
}.AsEnumerable();
keyValueTypeData.Execute();//调用Execute
var nonKeyValueTypeData=new[]{new{Name=“ITWeiHan”};
nonKeyValueTypeData.Execute();//调用Execute
}
公共静态类测试
{
公共静态void Execute(此IEnumerable枚举){}
公共静态void Execute(此IEnumerable枚举){}
}

一个
字典的数组是而不是
键值对的
IEnumerable
(一个
字典是-但这不是你拥有的)

我想你想做的是:

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();
        }
    }
}
使用System.Collections.Generic;
名称空间测试
{
公共静态类测试
{
公共静态void Execute(此IEnumerable枚举)
{
}
公共静态void Execute(此IEnumerable枚举)
{
}
}
公共课程
{
公共静态void Main()
{
IEnumerable data=new[]{
新字典(){
{“姓名”,“ITWeiHan”}
}
};
data.Execute();
}
}
}
请注意,解决方案的一部分是明确说明类型,以确保“鼓励”编译器选择我希望它选择的方法


i、 e.我使用
IEnumerable
而不是
var

一个
Dictionary
数组是而不是
KeyValuePair
IEnumerable
(一个
Dictionary
是-但这不是你拥有的)

我想你想做的是:

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();
        }
    }
}
使用System.Collections.Generic;
名称空间测试
{
公共静态类测试
{
公共静态void Execute(此IEnumerable枚举)
{
}
公共静态void Execute(此IEnumerable枚举)
{
}
}
公共课程
{
公共静态void Main()
{
IEnumerable data=new[]{
新字典(){
{“姓名”,“ITWeiHan”}
}
};
data.Execute();
}
}
}
请注意,解决方案的一部分是明确说明类型,以确保“鼓励”编译器选择我希望它选择的方法

i、 e.我使用
IEnumerable
而不是
var

尝试以下方法:

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>
}
static void Main()
{
var keyValueTypeData=new[]{
新词典(){{“Name”,“ITWeiHan”}
};
keyValueTypeData.SelectMany(x=>x.AsEnumerable()).Execute();//调用Execute
var nonKeyValueTypeData=new[]{new{Name=“ITWeiHan”};
nonKeyValueTypeData.Execute();//调用Execute
}
注意使用
SelectMany()
AsEnumerable()

尝试以下方法:

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>
}
static void Main()
{
var keyValueTypeData=new[]{
新词典(){{“Name”,“ITWeiHan”}
};
keyValueTypeData.SelectMany(x=>x.AsEnumerable()).Execute();//调用Execute
var nonKeyValueTypeData=new[]{new{Name=“ITWeiHan”};
nonKeyValueTypeData.Execute();//调用Execute
}

注意
SelectMany()
AsEnumerable()

字典数组不是KVP的ienumerable。你怎么会这么认为呢?我使用ToList()或.AsEnumerable(),它还调用Execute(这个IEnumerable枚举){}不管你对它做什么,只是不是这样。一袋鸡蛋盒不是鸡蛋的集合。这是一堆鸡蛋。@mjwills是的,你说得对。我修正了我的问题。
IEnumerable枚举
字典数组不是KVP的IEnumerable。你怎么会这么认为呢?我使用ToList()或.AsEnumerable(),它还调用Execute(这个IEnumerable枚举){}不管你对它做什么,只是不是这样。一袋鸡蛋盒不是鸡蛋的集合。这是一堆鸡蛋。@mjwills是的,你说得对。我修正了我的问题。
IEnumerable枚举