C# 将 List<ObjBase> 转换为 List<Obj>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1266014/
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
C# Casting a List<ObjBase> as List<Obj>
提问by AndrewHymansonZA
Why can I not cast a List<ObjBase>
as List<Obj>
? Why does the following not work:
为什么我不能将 aList<ObjBase>
作为List<Obj>
?为什么以下不起作用:
internal class ObjBase
{
}
internal class Obj : ObjBase
{
}
internal class ObjManager
{
internal List<Obj> returnStuff()
{
return getSomeStuff() as List<Obj>;
}
private List<ObjBase> getSomeStuff()
{
return new List<ObjBase>();
}
}
Instead I have to do this:
相反,我必须这样做:
internal class ObjBase
{
}
internal class Obj : ObjBase
{
}
internal class ObjManager
{
internal List<Obj> returnStuff()
{
List<ObjBase> returnedList = getSomeStuff();
List<Obj> listToReturn = new List<Obj>(returnedList.Count);
foreach (ObjBase currentBaseObject in returnedList)
{
listToReturn.Add(currentBaseObject as Obj);
}
return listToReturn;
}
private List<ObjBase> getSomeStuff()
{
return new List<ObjBase>();
}
}
I get the following error in Visual Studio 2008 (shortened for readability):
我在 Visual Studio 2008 中收到以下错误(为了可读性而缩短):
Cannot convert type 'List' to 'List' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
无法通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换将类型“List”转换为“List”
Thanks.
谢谢。
采纳答案by gjutras
回答by Lazarus
I think you are misunderstanding the cast you are trying to do. You are thinking that you are changing the type of the object that is stored in the list, where you are actually trying to change the type of the list itself. It rather makes sense that you can't change the list itself as you have already populated it.
我认为你误解了你想要做的演员。您认为您正在更改存储在列表中的对象的类型,实际上您正在尝试更改列表本身的类型。更有意义的是,您无法更改列表本身,因为您已经填充了它。
You might look at it as a list of a base class and then cast it when you are processing the list items, that would be my approach.
您可以将其视为基类的列表,然后在处理列表项时将其强制转换,这就是我的方法。
What is the purpose of this attempted cast?
这个尝试演员的目的是什么?
回答by Jimmeh
Linq has a ConvertAll method. so something like
Linq 有一个 ConvertAll 方法。所以像
list.ConvertAll<Obj>(objBase => objbase.ConvertTo(obj));
I'm not sure what else to suggest. I assume ObjBase is the base class, and if all ObjBase objects are Obj objects, i'm not sure why you would have the two objects in the first place. Perhaps i'm off the mark.
我不知道还有什么建议。我假设 ObjBase 是基类,如果所有 ObjBase 对象都是 Obj 对象,我不知道为什么你首先会有这两个对象。也许我不合时宜。
Edit: the list.Cast method would work better than the above, assuming they are castable to each other. Forgot about that until I read the other answers.
编辑: list.Cast 方法会比上面的方法工作得更好,假设它们可以相互转换。在我阅读其他答案之前忘记了这一点。
回答by Dewfy
Covariance my friend. Look at http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-contravariance
协方差我的朋友。看http://blog.tlk.com/dot-net/2009/c-sharp-4-covariance-and-contravariance
回答by Joachim Sauer
I can only describe the "problem" from a Java view, but from what little I know this aspect is the same in both C# and Java:
我只能从 Java 的角度描述“问题”,但据我所知,这方面在 C# 和 Java 中是相同的:
A List<ObjBase>
is not a List<Obj>
, because it could contain an ObjBase
object which is not a Obj
object.
AList<ObjBase>
不是 a List<Obj>
,因为它可能包含一个ObjBase
不是Obj
对象的对象。
The other way around a List<Obj>
can notbe cast to a List<ObjBase>
because the former guarantees to accept an Add()
call with a ObjBase
argument, which the latter will not accept!
周围的其他方式List<Obj>
可以不强制转换为List<ObjBase>
,因为前者保证接受的Add()
与呼叫ObjBase
参数,后者将不接受!
So to summarize: even though a Obj
is-a ObjBase
a List<Obj>
is nota List<ObjBase>
.
因此,要总结:即使Obj
是-一个ObjBase
一个List<Obj>
是不是一个List<ObjBase>
。
回答by Scott Ivey
回答by RaYell
You can use Cast
and ToList
extension methods from System.Linq to have this in one line.
您可以使用Cast
和ToList
从System.Linq的扩展方法有这样的一条线。
Instead of
代替
internal List<Obj> returnStuff()
{
return getSomeStuff() as List<Obj>;
}
do this:
做这个:
internal List<Obj> returnStuff()
{
return getSomeStuff().Cast<Obj>().ToList();
}
回答by Grzenio
This is a major pain in C# - this is how generics were designed. List doesn't extend List, its just a completely different type. You can't cast or assign them to each other in any way, your only option is to copy one list to the other one.
这是 C# 中的一大痛点——这就是泛型的设计方式。List 没有扩展 List,它只是一个完全不同的类型。您不能以任何方式将它们投射或分配给彼此,您唯一的选择是将一个列表复制到另一个列表。
回答by AndrewHymansonZA
Lazarus:
I thought that the compiler would realise that I wanted actions done on the objects of the list and not that I was trying to cast the list itself.
Lazarus:
我认为编译器会意识到我想要对列表的对象执行操作,而不是我试图强制转换列表本身。
Some more information:
更多信息:
public abstract class ObjBase
{
}
internal interface IDatabaseObject
{
}
public class Obj : ObjBase, IDatabaseObject
{
}
internal interface IDatabaseObjectManager
{
List<ObjBase> getSomeStuff();
}
public class ObjManager : IObjManager
{
public List<Obj> returnStuff()
{
return getSomeStuff().Cast <Customer>().ToList<Customer>();
}
private List<ObjBase> getSomeStuff()
{
return new List<ObjBase>();
}
}
Now client code outside of this DLL can go:
ObjManager objM = new ObjManager();
List listOB = objM.returnStuff();
I'm going to be creating several Obj
and ObjManager
types for this part (O/RM) of the application.
现在这个 DLL 之外的客户端代码可以去: ObjManager objM = new ObjManager(); 列表 listOB = objM.returnStuff(); 我将为应用程序的这部分 (O/RM)创建多个Obj
和ObjManager
类型。
(Darn comment block ran out of characters! :-)
(该死的评论块用完了字符!:-)
回答by Andrew Marais
Here is how I fixed the Conversion from a
这是我修复转换的方法
list<SomeOtherObject>
to a
到
object
and then to a
然后到一个
List<object>