Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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# 如何将IList转换回列表<;T>;在BindingSource中? BindingSource=newbindingsource(); 添加(新列表(){“1”、“2”、“3”}); //List theList=source.List//编译失败。无法从IList转换为列表隐式 列表列表=源。列表为列表//失败,无效_C# - Fatal编程技术网

C# 如何将IList转换回列表<;T>;在BindingSource中? BindingSource=newbindingsource(); 添加(新列表(){“1”、“2”、“3”}); //List theList=source.List//编译失败。无法从IList转换为列表隐式 列表列表=源。列表为列表//失败,无效

C# 如何将IList转换回列表<;T>;在BindingSource中? BindingSource=newbindingsource(); 添加(新列表(){“1”、“2”、“3”}); //List theList=source.List//编译失败。无法从IList转换为列表隐式 列表列表=源。列表为列表//失败,无效,c#,C#,我在网上看到人们创建了一个方法来执行显式转换。对于这项任务来说,这似乎是大刀阔斧。有没有更好的方法取回我的列表?您正在添加一个列表作为列表中的第一项 要检索它,您需要编写 BindingSource source = new BindingSource(); source.Add(new List<string>() { "1", "2", "3" }); //List<string> theList = source.List;//compi

我在网上看到人们创建了一个方法来执行显式转换。对于这项任务来说,这似乎是大刀阔斧。有没有更好的方法取回我的列表?

您正在添加一个
列表作为列表中的第一项

要检索它,您需要编写

    BindingSource source = new BindingSource();
    source.Add(new List<string>() { "1", "2", "3" });

     //List<string> theList = source.List;//compile fail.  Can't convert from IList to List<T> implicity
     List<string> theList = source.List as List<string>;//fail, null
(List)source.List[0];

如果您通过设置
数据源
属性实际绑定到
列表
,则代码可以工作

(List<string>) source.List[0];