C# 对象到字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10745542/
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
object to string array
提问by swcraft
I am trying to convert an object (is declared here as 'obj': object is array, primitive) to a string array.
我正在尝试将对象(在此处声明为“obj”:对象是数组,原始对象)转换为字符串数组。
object can be anything uint[], int16[], etc.
对象可以是任何 uint[]、int16[] 等。
I have been trying to use
我一直在尝试使用
string[] str = Array.ConvertAll<object, string>((object[])obj, Convert.ToString);
The problem occurs when I try to cast the unknown type object into object[]. I have been getting casting error.
当我尝试将未知类型的对象转换为 object[] 时会出现问题。我一直收到铸造错误。
One attempt I made, which failed, was using
我所做的一项失败的尝试是使用
object[] arr = (object[])obj;
or
或者
IEnumerable<object> list = obj as IEnumerable<object>
object[] arr = (object[])list;
I saw postings regarding value type and reference type issue on casting.
我在转换时看到了有关值类型和引用类型问题的帖子。
Would there be a simple code that can handle casting to object[] regardless of type of object, as long as it is an array ? I am trying to avoid manual handling of every possible type casting.
是否有一个简单的代码可以处理转换为 object[] 而不管对象的类型,只要它是一个数组?我试图避免手动处理每种可能的类型转换。
thanks in advance
提前致谢
采纳答案by Jon Skeet
You can use the fact that every array implements IEnumerable:
您可以使用每个数组都实现的事实IEnumerable:
string[] arr = ((IEnumerable)obj).Cast<object>()
.Select(x => x.ToString())
.ToArray();
This will box primitives appropriately, before converting them to strings.
在将它们转换为字符串之前,这将适当地装箱原语。
The reason the cast fails is that although arrays of referencetypes are covariant, arrays of valuetypes are not:
转换失败的原因是虽然引用类型的数组是协变的,但值类型的数组不是:
object[] x = new string[10]; // Fine
object[] y = new int[10]; // Fails
Casting to just IEnumerablewill work though. Heck, you could cast to Arrayif you wanted.
投射到只是IEnumerable会起作用。哎呀,Array如果你愿意,你可以投射到。
回答by JaredPar
If it's always a collection of some type (array, list, etc ...) then try casting back to plain old System.Collections.IEnumerableand go from there
如果它始终是某种类型(数组、列表等...)的集合,那么尝试转换回普通的旧System.Collections.IEnumerable并从那里开始
string[] str = ((System.Collections.IEnumerable)obj)
.Cast<object>()
.Select(x => x.ToString())
.ToArray();
Here is a more thorough implementation that handles non-collections as well
这是一个更彻底的实现,它也处理非集合
static string[] ToStringArray(object arg) {
var collection = arg as System.Collections.IEnumerable;
if (collection != null) {
return collection
.Cast<object>()
.Select(x => x.ToString())
.ToArray();
}
if (arg == null) {
return new string[] { };
}
return new string[] { arg.ToString() };
}
回答by S.Hafner
my example:
我的例子:
public class TestObject
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public string Property3 { get; set; }
}
static void Main(string[] args)
{
List<TestObject> testObjectList = new List<TestObject>
{
new TestObject() { Property1 = "1", Property2 = "2", Property3 = "3" },
new TestObject() { Property1 = "1", Property2 = "2", Property3 = "3" },
new TestObject() { Property1 = "1", Property2 = "2", Property3 = "3" }
};
List<string[]> convertedTestObjectList = testObjectList.Select(x => new string[] { x.Property1, x.Property2, x.Property3 }).ToList();
}

