将数组字符串转换为对象数组的简单方法? [英] Easy way to cast an array of strings to an array of objects?
问题描述
我需要将一个字符串[]转换为一个对象[],反之亦然。
目前,我正在使用这样的代码:
int numOfObjects = Values.Length;
object [] objects = new object [numOfObjects];
for(int i = 0; i< ; numOfObjects; i ++){
objects [i] =(object)values [i];
}
where values是一个字符串[]传递给进行转换的成员。
它有效,但看起来很笨重。我觉得应该有一个更简单的方法。
有吗?
I need to cast a string[] into an object[] and vice versa.
At the moment, I''m using code like this:
int numOfObjects = Values.Length;
object[] objects = new object[numOfObjects];
for(int i = 0; i < numOfObjects; i++) {
objects[i] = (object)Values[i];
}
where Values is a string[] passed to the member doing the conversion.
It works but it seems clunky. I feel there should be an easier way.
Is there?
推荐答案
use(object [] )stringArray
string [] stringItems =" a,b,c,d,e" .Split('','');
object [] objectItems =(object [])stringItems;
use (object[])stringArray
string[] stringItems = "a,b,c,d,e".Split('','');
object[] objectItems = (object[])stringItems;
你不能把字符串[]强制转换为object [],但是你可以做的是使用
Array.Copy函数而不是自己的循环。
" ssg31415926"写道:
you can''t cast string[] to object[], but what you can do is use the
Array.Copy function instead of doing your own loop.
"ssg31415926" wrote:
我需要将一个字符串[]转换为一个对象[],反之亦然。
目前,我正在使用代码这个:
int numOfObjects = Values.Length;
object [] objects = new object [numOfObjects];
for(int i = 0; i< numOfObjects; i ++) {
objects [i] =(object)Values [i];
}
其中Values是传递给执行转换的成员的字符串[]。
它有效,但看起来很笨重。我觉得应该有一个更简单的方法。
有吗?
I need to cast a string[] into an object[] and vice versa.
At the moment, I''m using code like this:
int numOfObjects = Values.Length;
object[] objects = new object[numOfObjects];
for(int i = 0; i < numOfObjects; i++) {
objects[i] = (object)Values[i];
}
where Values is a string[] passed to the member doing the conversion.
It works but it seems clunky. I feel there should be an easier way.
Is there?
对不起,我把它拿回去,你实际上可以演员string [] to object []。
" Daniel Jin"写道:
sorry, I take that back, you can actually cast string[] to object[].
"Daniel Jin" wrote:
你不能把字符串[]强制转换为object [],但是你能做的就是使用
Array.Copy函数而不是自己做循环。
" ssg31415926"写道:
you can''t cast string[] to object[], but what you can do is use the
Array.Copy function instead of doing your own loop.
"ssg31415926" wrote:
我需要将一个字符串[]转换为一个对象[],反之亦然。
目前,我正在使用代码这个:
int numOfObjects = Values.Length;
object [] objects = new object [numOfObjects];
for(int i = 0; i< numOfObjects; i ++) {
objects [i] =(object)Values [i];
}
其中Values是传递给执行转换的成员的字符串[]。
它有效,但看起来很笨重。我觉得应该有一个更简单的方法。
有吗?
I need to cast a string[] into an object[] and vice versa.
At the moment, I''m using code like this:
int numOfObjects = Values.Length;
object[] objects = new object[numOfObjects];
for(int i = 0; i < numOfObjects; i++) {
objects[i] = (object)Values[i];
}
where Values is a string[] passed to the member doing the conversion.
It works but it seems clunky. I feel there should be an easier way.
Is there?
这篇关于将数组字符串转换为对象数组的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!