C# 如何将对象/结构转换为数组列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/625945/
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
How to cast object/structure into arraylist?
提问by ihcarp
I have a structure in C#.net. I have created an object of that structure. I need to cast this object of structure into ArrayList. I am using :
我在 C#.net 中有一个结构。我已经创建了该结构的对象。我需要将此结构对象转换为 ArrayList。我在用 :
OrderMessageStructure.OrderMessage orderToModify = new OrderMessageStructure.OrderMessage();
object neworderobject = (object)orderToModify;
ArrayList arr = new ArrayList();
arr = (ArrayList)neworderobject;
But I am getting error "Unable to cast object of type 'OrderMessage' to type 'System.Collections.ArrayList'."
但我收到错误“无法将类型为 'OrderMessage' 的对象转换为类型为 'System.Collections.ArrayList'。”
How to convert object/structure into ArrayList?
如何将对象/结构转换为 ArrayList?
采纳答案by RS Conley
Your question doesn't have quite enough information to give a sure answer.
你的问题没有足够的信息来给出肯定的答案。
If you want to make an ArrayList of OrderMessage objects then you need to create the arraylist FIRST and then ADD instances of OrderMessage.
如果要创建 OrderMessage 对象的 ArrayList,则需要首先创建 arraylist,然后添加 OrderMessage 的实例。
If you want a strongly typed ArrayList of OrderMessage then use List from the Generic Namespace and ADD instances of OrderMessage to it.
如果您想要 OrderMessage 的强类型 ArrayList,则使用来自通用命名空间的 List 并将 OrderMessage 的 ADD 实例添加到它。
If your OrderMessage has an array structure in it then you will need to add a method to it that returns an arraylist. You will have to write the code converting the OrderMessage to an ArrayList.
如果您的 OrderMessage 中有一个数组结构,那么您需要向它添加一个返回数组列表的方法。您必须编写将 OrderMessage 转换为 ArrayList 的代码。
回答by ihcarp
You can't just cast any object into another object in C#. Its a type safe language.
您不能在 C# 中将任何对象强制转换为另一个对象。它是一种类型安全的语言。
You need to create an ArrayList
and Add()
your struct to it.
您需要为它创建一个ArrayList
和Add()
您的结构。
Better yet, create a List<StructType>()
and add your struct to that.
更好的是,创建一个List<StructType>()
并将您的结构添加到其中。
CLR Via C#. Read it.
CLR 通过 C#。阅读。
OrderMessageStructure.OrderMessage orderToModify =
new OrderMessageStructure.OrderMessage();
ArrayList arr = new ArrayList();
arr.Add(orderToModify);
回答by JaredPar
You cannot convert these two objects because they have completely unrelated types. In order for a cast to succeed, OrderMessage must inherit from ArrayList which is almost certainly not what you want. It's more likely that you want to add an OrderMessage into an ArrayList which can be done like so.
您不能转换这两个对象,因为它们具有完全不相关的类型。为了使转换成功,OrderMessage 必须从 ArrayList 继承,这几乎肯定不是您想要的。您更有可能希望将 OrderMessage 添加到 ArrayList 中,可以这样做。
ArrayList arr = new ArrayList();
arr.Add(orderToModify);
回答by andleer
No need to cast objects. Simply add them to your arraylist:
无需投射对象。只需将它们添加到您的数组列表中:
ArrayList list = new ArrayList();
list.Add(myObject);
but a better option is to use a generic List:
但更好的选择是使用通用列表:
List<MyObject> list = new List<MyOjbect>();
list.Add(MyObject);
The advantage of the generic list is that it is strongly typed so you won't have to cast the object back to your type when pulling it out of your list.
泛型列表的优点是它是强类型的,因此当将它从列表中拉出时,您不必将对象强制转换回您的类型。