C# 将 List<List<object>> 转换为 IList<IList<object>>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9005944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:33:42  来源:igfitidea点击:

convert List<List<object>> to IList<IList<object>>

c#.netlist

提问by david

I have written a method which is public List<List<object>> Fetch(string data), inside I create List<List<object>> p = new List<List<object>>();

我写了一个方法 public List<List<object>> Fetch(string data),在里面我创建 List<List<object>> p = new List<List<object>>();

my boss now wants to return a IList<IList<object>>instead of List<List<object>>ie
public IList<IList<object>> Fetch(string data),

我的老板现在想要返回一个IList<IList<object>>而不是List<List<object>>ie
public IList<IList<object>> Fetch(string data)

so when I try do return (IList<IList<object>>) p; //throws an exception

所以当我尝试做 return (IList<IList<object>>) p; //throws an exception

How do I convert List<List<object>>to IList<IList<object>>and back to List<List<object>>

我如何转换 List<List<object>>IList<IList<object>>和返回List<List<object>>

采纳答案by Jon Skeet

You can't perform that conversion via straight casting - it wouldn't be safe. Instead, you should use:

你不能通过直接转换来执行这种转换——这不安全。相反,您应该使用:

IList<IList<object>> ret = new List<IList<object>>();

Then for each "sublist" you can use:

然后对于每个“子列表”,您可以使用:

// Or whatever
ret.Add(new List<object>());

Finally, just return ret.

最后,只需 return ret

You coulduse LINQ to perform the conversion of your existing List<List<object>>when you return it - but it would be better to just create a more appropriate type to start with, as shown above.

可以List<List<object>>在返回时使用 LINQ 来执行现有类型的转换- 但最好先创建一个更合适的类型,如上所示。



To understand why some of the existing answers are wrong, suppose you coulddo this:

要了解为什么某些现有答案是错误的,假设您可以这样做:

IList<IList<object>> p = new List<List<object>>();

Then this would be valid:

那么这将是有效的:

List<List<object>> listOfLists = new List<List<object>>();
IList<IList<object>> p = listOfLists;
p.Add(new object[]);
List<object> list = p[0];

But p[0]is a reference to an object[], not a List<object>... our supposedly type-safe code doesn't look as safe any more...

但是p[0]是对 an 的引用object[],而不是List<object>...我们所谓的类型安全代码看起来不再那么安全了...

Fortunately, IList<T>is invariant to prevent exactly this problem.

幸运的是,IList<T>不变式正好防止了这个问题。

回答by Aliostad

List<T>and IList<T>do not support covariance. So this does not compile:

List<T>并且IList<T>不支持协方差。所以这不会编译:

List<List<string>> list = new List<IList<string>>(); // DOES NOT COMPILE!!!!!

回答by Chris Shain

var myOriginalList = new List<List<Object>>();
var converted = ((IList<IList<Object>>)myOriginalList.Cast<IList<Object>>().ToList()); // EDIT: Added .ToList() here

You shouldn't need to convert back- you can do just about anything on IListthat you could on List.

你不应该需要转换的后台,你可以做几乎任何事情IList,你可以上List

回答by John Ruiz

public IList<IList<object>> Fetch(string data)
{
  IList<IList<object>> p = new List<IList<object>>();

  // add your stuff here

  return p;
}

回答by Olivier Jacot-Descombes

You would have to declare your list as

您必须将列表声明为

IList<IList<object>> list = new List<IList<object>>(); // Works!

This works, because only the outer list is created in the first place. You can then insert individual items that are compatible with IList<object>:

这是有效的,因为首先只创建了外部列表。然后,您可以插入与 兼容的单个项目IList<object>

list.Add(new List<object>());
list.Add(new object[10]);

回答by Muhammad Hasan Khan

You need to change the declaration of the result variable from List<List<object>to IList<IList<object>>

您需要将结果变量的声明从List<List<object>更改为IList<IList<object>>

Which you can instantiate against List<IList<object>>

你可以实例化 List<IList<object>>

And each item in the result can be of type List<object>

结果中的每个项目都可以是类型 List<object>

    static IList<IList<object>> Test()
    {
        IList<IList<object>> result = new List<IList<object>>();
        var item = new List<object>() { "one", "two", "three" };
        result.Add(item);
        return result;
    }