[英]C# Accessing data in System.Object[]
I'm coding in C# and I have a dictionary with tons of data. 我正在用C#编写代码,并且有一个包含大量数据的字典。 One of the members is "children" but when I try to write out its values, I get: System.Object[]
成员之一是“孩子”,但是当我尝试写出它的值时,我得到:System.Object []
I know that children contains data, probably nested data, but I'm unsure if it's a list, dictionary, array, etc. 我知道孩子包含数据,可能是嵌套数据,但是我不确定它是否是列表,字典,数组等。
How do I write out all the data within "children"? 如何写出“儿童”中的所有数据?
The default response of any instantiated .NET type to "ToString()" is to write out the fully qualified type name. 任何实例化的.NET类型对“ ToString()”的默认响应是写出完全限定的类型名称。
System.Object[] means that you have an array where each element is of type "Object". System.Object []意味着您有一个数组,其中每个元素的类型均为“ Object”。 This "box" could contain anything since every type in .NET derives from Object.
此“框”可以包含任何内容,因为.NET中的每种类型都源自Object。 The following may tell you what the array really contains in terms of instantiated types:
以下可能会告诉您根据实例化类型,数组真正包含的内容:
foreach (object o in children)
Console.WriteLine(o != null ? o.GetType().FullName : "null");
It it an array of object
references, so you will need to iterate over it and extract the objects, For example: 它是
object
引用的数组,因此您将需要对其进行迭代并提取对象,例如:
// could also use IEnumerable or IEnumerable<object> in
// place of object[] here
object[] arr = (object[])foo["children"];
foreach(object bar in arr) {
Console.WriteLine(bar);
}
If you know what the objects are, you can cast etc - or you can use the LINQ OfType/Cast extension methods: 如果知道对象是什么,则可以进行强制转换-或可以使用LINQ OfType / Cast扩展方法:
foreach(string s in arr.OfType<string>()) { // just the strings
Console.WriteLine(s);
}
Or you can test each object: 或者您可以测试每个对象:
foreach(object obj in arr) { // just the strings
if(obj is int) {
int i = (int) obj;
//...
}
// or with "as"
string s = obj as string;
if(s != null) {
// do something with s
}
}
Beyond that, you are going to have to add more detail... 除此之外,您将不得不添加更多细节...
I realize that this thread is over a year old, but I wanted to post a solution I found in case anyone is wrestling with trying to get data out of the System.Object[] returned using the Cook Computing XML-RPC Library. 我意识到该线程已经使用了一年多,但是我想发布一个解决方案,以防万一有人试图从使用Cook Computing XML-RPC库返回的System.Object []中获取数据。
Once you have the Children object returned, use the following code to view the key/values contained within: 一旦返回了Children对象,请使用以下代码查看其中包含的键/值:
foreach (XmlRpcStruct rpcstruct in Children)
{
foreach (DictionaryEntry de in rpcstruct)
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
Console.WriteLine();
}
(Note I didn't test this code in VS, working off memory here). (请注意,我没有在VS中测试此代码,而是在这里处理内存)。
object[] children = (object[])foo["children"];
foreach(object child in children)
System.Diagnostics.Debug.WriteLine(child.GetType().FullName);
This should dump out the classnames of the children. 这应该转储孩子的类名。
If you are doing a foreach on foo["children"] you shouldn't be getting a failure that a public iterator isn't found since by definition an array has one (unless I missed something). 如果您要对foo [“ children”]进行foreach操作,那么就不会因为找不到数组而失败,因为按照定义,数组有一个(除非我错过了什么)。
"I know that children contains data, probably nested data, but I'm unsure if it's a list, dictionary, array, etc" “我知道子级包含数据,可能是嵌套数据,但是我不确定它是否是列表,字典,数组等”
So the childreen is IEnumerable or not a collection 所以childreen是IEnumerable或不是集合
try this code 试试这个代码
void Iterate(object childreen)
{
if(data is IEnumerable)
foreach(object item in data)
Iterate(item);
else Console.WriteLine(data.ToString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.