创建动态数组...... [英] Creating a dynamic array...

查看:77
本文介绍了创建动态数组......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我已经想出如何创建一个对象的实例,只知道

类型的字符串。


string sName =" MyClassName";

类型t = Type.GetType(sName);

Object objNew = Activator.CreateInstance(t);


这样可行,但现在我需要声明一个像
这样的数组

Object [] objNew = Activator.CreateInstance(t)[0] ;


这不行。任何人都可以帮忙吗?


谢谢。


Matthew Wells
Ma *********** @ FirstByte.net

解决方案

< blockquote> Matt,


我相信这就是你要找的东西。


数组数组= Array.CreateInstance(类型,长度) );


Jason Newell

软件工程师
www.jasonnewell.net


Matthew Wells写道:


你好。


我已经想出如何创建一个对象的实例,只知道

类型的字符串。


string sName =" MyClassName";

Type t = Type.GetType(sName);

Object objNew = Activator.CreateInstance(t);


这样可行,但现在我需要声明一个数组,如


Object [] objNew = Activator.Creat eInstance(t)[0];


这不行。任何人都可以帮忙吗?


谢谢。


Matthew Wells
Ma *********** @ FirstByte.net

不完全。这仍然返回一个Array类型,而不是Object类型。 。我需要

来获得相当于这些陈述,好像我知道我有什么类型

开头。


public MyClass Person

{

string name;

}


Person [] = new Person [ 2];


问题是我不知道我需要什么类型。当我尝试分配人时,您给出的代码

会返回错误在

数组中的元素。 - 不能将带有[]的索引应用于类型为
的表达式
''System.Array''"我不能投,因为我不知道它是什么类型。


这绝对是朝向目标。还有更多想法吗?

" Jason Newell" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP02.phx.gbl ...


Matt,


我相信这就是你要找的东西。


数组数组= Array.CreateInstance(类型,长度);


Jason Newell

软件工程师
www.jasonnewell.net


Matthew Wells写道:
< blockquote class =post_quotes>
> Hello。

我已经想出如何创建一个对象的实例,只知道按字符串类型的类型。

string sName =" MyClassName";
Type t = Type.GetType(sName);
Object objNew = Activator.CreateInstance(t);

这个有效,但现在我需要声明一个数组,如

对象[] objNew = Activator.CreateInstance(t)[0];

这不行。任何人都可以帮忙吗?

谢谢。

马修威尔斯
Ma *********** @ FirstByte.net


坚持 - 最初你说你只知道字符串的类型;你知道在这种情况下
会比Array好得多(注意

数组在这种情况下有GetValue()和SetValue())。


另一种选择可能涉及泛型。


您可以使用一些技巧从字符串中获取类型;

类型。 GetType(name)有时会起作用,但它取决于类

的位置......你可能需要枚举一点......


来自a输入,您可以使用MakeGenericMethod()调用泛型方法。


类似于:


class Person {}

static class Program {

static void Main(){

Person [] typed = SomeMethod< Person>();

Person p1 = typed [3];


类型t = Type.GetType(" Person");

数组无类型=(数组)

typeof(Program).GetMethod(&Some SomeSethod")。MakeGeneri cMethod(t).Invoke(null,

null);

Person p2 =(Person )untyped.GetValue(3);

}

public static T [] SomeMethod< T>()其中T:class,new(){

T [] data = new T [10] ;

for(int i = 0;我< 10; i ++){

data [i] = new T();

}

返回数据;

}

}


Hello.

I have figured out how to create an instance of an object only knowing the
type by string.

string sName = "MyClassName";
Type t = Type.GetType(sName);
Object objNew = Activator.CreateInstance(t);

This works, but now I need to declare an array like

Object[] objNew = Activator.CreateInstance(t)[0];

This doesn''t work. Can anyone help?

Thanks.

Matthew Wells
Ma***********@FirstByte.net

解决方案

Matt,

I believe this is what you''re looking for.

Array array = Array.CreateInstance(type, length);

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew Wells wrote:

Hello.

I have figured out how to create an instance of an object only knowing the
type by string.

string sName = "MyClassName";
Type t = Type.GetType(sName);
Object objNew = Activator.CreateInstance(t);

This works, but now I need to declare an array like

Object[] objNew = Activator.CreateInstance(t)[0];

This doesn''t work. Can anyone help?

Thanks.

Matthew Wells
Ma***********@FirstByte.net


Not quite. This still returns an Array type, not an Object type. . I need
to have the equivalent of these statements as if I knew what type I had to
start with.

public MyClass Person
{
string Name;
}

Person[] = new Person[2];

The problem is I don''t know what type I will need. The code you gave
returns an error when I try to assign a "person" to an elememt in the
array. - "Can''t apply indexing with [] to an expression of type
''System.Array''" I can''t cast it because I don''t know what type it is.

This is definitely towards the goal. Any more ideas?
"Jason Newell" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

Matt,

I believe this is what you''re looking for.

Array array = Array.CreateInstance(type, length);

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew Wells wrote:

>Hello.

I have figured out how to create an instance of an object only knowing
the type by string.

string sName = "MyClassName";
Type t = Type.GetType(sName);
Object objNew = Activator.CreateInstance(t);

This works, but now I need to declare an array like

Object[] objNew = Activator.CreateInstance(t)[0];

This doesn''t work. Can anyone help?

Thanks.

Matthew Wells
Ma***********@FirstByte.net


Hang on - originally you said you only knew the type as a string; you
will struggle to get much better than Array in this case (note that
Array has GetValue() and SetValue() to work in this case).

Another option might involve generics.

You can get a Type from a string using a few tricks;
Type.GetType(name) sometimes works, but it depends on where the class
is... you may need to enumerate a little...

From a Type, you can call a generic method using MakeGenericMethod().

Something like:

class Person { }
static class Program {
static void Main() {
Person[] typed = SomeMethod<Person>();
Person p1 = typed[3];

Type t = Type.GetType("Person");
Array untyped = (Array)
typeof(Program).GetMethod("SomeMethod").MakeGeneri cMethod(t).Invoke(null,
null);
Person p2 = (Person) untyped.GetValue(3);
}
public static T[] SomeMethod<T>() where T : class, new() {
T[] data = new T[10];
for (int i = 0; i < 10; i++) {
data[i] = new T();
}
return data;
}
}


这篇关于创建动态数组......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆