C# 如何将对象转换为对象[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9911141/
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 convert object to object[]
提问by Rick Mohr
I have an objectwhose value may be one of several array types like int[]or string[], and I want to convert it to a string[]. My first attempt failed:
我有一个,object它的值可能是像int[]or这样的几种数组类型之一string[],我想将它转换为string[]. 我的第一次尝试失败了:
void Do(object value)
{
if (value.GetType().IsArray)
{
object[] array = (object[])value;
string[] strings = Array.ConvertAll(array, item => item.ToString());
// ...
}
}
with the runtime error Unable to cast object of type 'System.Int32[]' to type 'System.Object[]', which makes sense in retrospect since my int[]doesn't contain boxed integers.
带有运行时错误Unable to cast object of type 'System.Int32[]' to type 'System.Object[]',回想起来很有意义,因为我int[]不包含装箱整数。
After poking around I arrived at this working version:
在四处探索之后,我到达了这个工作版本:
void Do(object value)
{
if (value.GetType().IsArray)
{
object[] array = ((Array)value).Cast<object>().ToArray();
string[] strings = Array.ConvertAll(array, item => item.ToString());
// ...
}
}
I guess this is OK, but it seems pretty convoluted. Anyone have a simpler way?
我想这没问题,但看起来很复杂。有人有更简单的方法吗?
采纳答案by Jon Skeet
You don't need to convert it to an array and thenuse LINQ. You can do it in a more streaming fashion, only converting to an array at the end:
您不需要将其转换为数组然后使用 LINQ。您可以以更流式的方式执行此操作,仅在最后转换为数组:
var strings = ((IEnumerable) value).Cast<object>()
.Select(x => x == null ? x : x.ToString())
.ToArray();
(Note that this will preserve nulls, rather than throwing an exception. It's also fine for any IEnumerable, not just arrays.)
(请注意,这将保留空值,而不是抛出异常。它也适用于 any IEnumerable,而不仅仅是数组。)
回答by Servy
void Do(object value)
{
if (value.GetType().IsArray)
{
string[] strings = ((object[]) value).Select(obj => Convert.ToString(obj)).ToArray();
}
}
回答by Slai
.ToArraymakes multiple memory allocations in most cases, but there are few ways around it:
.ToArray在大多数情况下进行多次内存分配,但解决方法很少:
object value = new[] { 1, 2.3 };
IList list = value as IList;
string[] strings = new string[list.Count];
for (int i = 0; i < strings.Length; i++)
strings[i] = Convert.ToString(list[i]);
In most cases that might be a bit overkill and waste of vertical space, so I would use something like the accepted answer with an optional null-conditional operator ?to check if the source is array:
在大多数情况下,这可能有点矫枉过正和浪费垂直空间,所以我会使用类似接受的答案和可选的空条件运算符?来检查源是否为数组:
string[] strings = (value as Array)?.Cast<object>().Select(Convert.ToString).ToArray();
回答by Rishabh Agarwal
The solution below is in JAVA
下面的解决方案是在JAVA
public static Objects[] convertObjectToObjectArray(){
Objects firstObject = new Object();
return new Object[]{firstObject};
}
In the above return statement, you are converting an object to an object array.
在上面的 return 语句中,您将对象转换为对象数组。

