C# 访问 System.Object[] 中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/420275/
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-04 02:41:50  来源:igfitidea点击:

C# Accessing data in System.Object[]

c#

提问by

I'm coding in C# and I have a dictionary with tons of data. One of the members is "children" but when I try to write out its values, I get: System.Object[]

我正在用 C# 编码,我有一个包含大量数据的字典。其中一个成员是“孩子”,但是当我尝试写出它的值时,我得到: System.Object[]

I know that children contains data, probably nested data, but I'm unsure if it's a list, dictionary, array, etc.

我知道 children 包含数据,可能是嵌套数据,但我不确定它是列表、字典、数组等。

How do I write out all the data within "children"?

我如何写出“孩子”中的所有数据?

回答by flq

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". This "box" could contain anything since every type in .NET derives from Object. The following may tell you what the array really contains in terms of instantiated types:

System.Object[] 表示您有一个数组,其中每个元素的类型都是“Object”。这个“盒子”可以包含任何东西,因为 .NET 中的每种类型都源自 Object。以下内容可能会告诉您数组在实例化类型方面真正包含的内容:

foreach (object o in children)
  Console.WriteLine(o != null ? o.GetType().FullName : "null");

回答by Marc Gravell

It it an array of objectreferences, 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...

除此之外,您将不得不添加更多细节......

回答by Ken Henderson

(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,您不应该因为找不到公共迭代器而失败,因为根据定义,数组有一个(除非我遗漏了一些东西)。

回答by Ahmed Said

"I know that children contains data, probably nested data, but I'm unsure if it's a list, dictionary, array, etc"

“我知道 children 包含数据,可能是嵌套数据,但我不确定它是否是列表、字典、数组等”

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());
}

回答by Jimbo

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.

我意识到这个线程已经有一年多了,但我想发布一个我找到的解决方案,以防有人试图从使用库克计算 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();
        }