Creating a dynamic array...

M

Matthew Wells

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
(e-mail address removed)
 
M

Matthew Wells

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 said:
Matt,

I believe this is what you're looking for.

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

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew said:
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
(e-mail address removed)
 
M

Marc Gravell

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").MakeGenericMethod(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 = new T();
}
return data;
}
}
 
J

Jason Newell

Matt,

Have a look at this approach then.

ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add(5.3);
arrayList.Add(true);
arrayList.Add("Hello World");
object[] array = arrayList.ToArray();

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew said:
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 said:
Matt,

I believe this is what you're looking for.

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

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew said:
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
(e-mail address removed)
 
M

Matthew Wells

This is a little too much for what I need - and I don't think generics are
an option because I am also getting the type dynamically.

Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

This works to get one instance of my object, but in other cases I need to
get an array like

t[] MyList = new t[5]; - this fails (I've also added t.GetType() and
typeof(t) )

I also tried

List<t> = new List<t>(); - this also fails - and with thte same options as
above.

I don't think this works because 't' is not strongly typed.

Why is this so difficult?
 
J

Jon Skeet [C# MVP]

On Aug 30, 4:31 pm, "Matthew Wells" <[email protected]>
wrote:

I don't think this works because 't' is not strongly typed.

Why is this so difficult?

Because the type information is largely there for the *compiler*.
You're trying to provide it at runtime.

Could you try to explain *why* you believe you need this? There's
almost certainly a better approach to the problem.

Jon
 
M

Matthew Wells

I'm reading metadata I've netered into an Access table that defines objects.
Each object can have primitive property types like string, int, etc, but can
also have other objects defined as properties.. Here is one brief example.

Customer (entity)
FirstName
LastName
Address (entity)
Contacts[] (entity) - this is an array of contacts.
Contact (entity)
Address (entity)


I'm transferring data from one system to another. I have a recursive
function set up to read from the (in this case the customer table) create an
instance of the object, assign values to the properties, then create the
array and assign subentities to that and then assign the array to the parent
entity's property. The theory works fine. As I said, all the data is being
read from a table so I have no way of knowing what entities are needed ahead
of time. This works when I only need one instance of an entity (like the
Customer's Address entity) using

string sEntityName = "Contact"
Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

I've tried your suggestions of using an ArrayList and Array.CreateInstance.
These work so I can assign objects into them, but when it comes time to add
the array or list to the Customer entity, it fails because the Custoemr
entity is expecting an array of Contacts, not a generic Array or ArrayList.
I can't cast either because casting requires a known type. I've tried
(t.GetType()) and (typeof(t)) without success.

Does this make it clearer?
 
J

Jon Skeet [C# MVP]

Matthew Wells said:
I'm reading metadata I've netered into an Access table that defines objects.
Each object can have primitive property types like string, int, etc, but can
also have other objects defined as properties.. Here is one brief example.

Customer (entity)
FirstName
LastName
Address (entity)
Contacts[] (entity) - this is an array of contacts.
Contact (entity)
Address (entity)


I'm transferring data from one system to another. I have a recursive
function set up to read from the (in this case the customer table) create an
instance of the object, assign values to the properties, then create the
array and assign subentities to that and then assign the array to the parent
entity's property. The theory works fine. As I said, all the data is being
read from a table so I have no way of knowing what entities are needed ahead
of time. This works when I only need one instance of an entity (like the
Customer's Address entity) using

string sEntityName = "Contact"
Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

I've tried your suggestions of using an ArrayList and Array.CreateInstance.
These work so I can assign objects into them, but when it comes time to add
the array or list to the Customer entity, it fails because the Custoemr
entity is expecting an array of Contacts, not a generic Array or ArrayList.
I can't cast either because casting requires a known type. I've tried
(t.GetType()) and (typeof(t)) without success.

The way that the arrays are created with Jason's original code creates
the right type of array at runtime. Presumably you're using reflection
to set the property anyway, so it should be fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
M

Marc Gravell

and I don't think generics are
an option because I am also getting the type dynamically.
Except of course that this is in response to a post that does
*precisely* this.

Marc
 
M

Matthew Wells

I got it. The problem was in the type declaration. I also had to use the
..GetConstructor method. The type with an array is idfferent thatn

Type t = Type.GetType(stype) + "[]");
object objList = t.GetContructor(new Type[](typeof(int32))).invoke(new
Object[] (...element count...));

You can't assign to objList[0], but you can create a new object{] objListNew
and cast it to an array of objects. Then I can still assign objList to the
main entity's property that is expecting an array of type t.



Jon Skeet said:
Matthew Wells said:
I'm reading metadata I've netered into an Access table that defines
objects.
Each object can have primitive property types like string, int, etc, but
can
also have other objects defined as properties.. Here is one brief
example.

Customer (entity)
FirstName
LastName
Address (entity)
Contacts[] (entity) - this is an array of contacts.
Contact (entity)
Address (entity)


I'm transferring data from one system to another. I have a recursive
function set up to read from the (in this case the customer table) create
an
instance of the object, assign values to the properties, then create the
array and assign subentities to that and then assign the array to the
parent
entity's property. The theory works fine. As I said, all the data is
being
read from a table so I have no way of knowing what entities are needed
ahead
of time. This works when I only need one instance of an entity (like the
Customer's Address entity) using

string sEntityName = "Contact"
Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

I've tried your suggestions of using an ArrayList and
Array.CreateInstance.
These work so I can assign objects into them, but when it comes time to
add
the array or list to the Customer entity, it fails because the Custoemr
entity is expecting an array of Contacts, not a generic Array or
ArrayList.
I can't cast either because casting requires a known type. I've tried
(t.GetType()) and (typeof(t)) without success.

The way that the arrays are created with Jason's original code creates
the right type of array at runtime. Presumably you're using reflection
to set the property anyway, so it should be fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jon Skeet [C# MVP]

Matthew Wells said:
I got it. The problem was in the type declaration. I also had to use the
.GetConstructor method. The type with an array is idfferent thatn

Type t = Type.GetType(stype) + "[]");
object objList = t.GetContructor(new Type[](typeof(int32))).invoke(new
Object[] (...element count...));

You can't assign to objList[0], but you can create a new object{] objListNew
and cast it to an array of objects. Then I can still assign objList to the
main entity's property that is expecting an array of type t.

What does that do that using Array.CreateInstance doesn't do rather
more simply?
 
J

Jason Newell

Matt,

I think non of us still get what you're asking for. I would stab my
eyes out if I had to work on the code in your last reply. I have a hard
time believing that there is not a more eloquent way to write the code,
but then again, I don't understand what you're after.

Surely you can come up with something better with this example.

Type t = typeof(string); // Or whatever
ArrayList arrayList = new ArrayList(Array.CreateInstance(t, 0));
arrayList.Add("Hello");
arrayList.Add("World");
object[] objList = arrayList.ToArray();
objList[0] = "Goodbye";

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew said:
I got it. The problem was in the type declaration. I also had to use the
.GetConstructor method. The type with an array is idfferent thatn

Type t = Type.GetType(stype) + "[]");
object objList = t.GetContructor(new Type[](typeof(int32))).invoke(new
Object[] (...element count...));

You can't assign to objList[0], but you can create a new object{] objListNew
and cast it to an array of objects. Then I can still assign objList to the
main entity's property that is expecting an array of type t.



Jon Skeet said:
Matthew Wells said:
I'm reading metadata I've netered into an Access table that defines
objects.
Each object can have primitive property types like string, int, etc, but
can
also have other objects defined as properties.. Here is one brief
example.

Customer (entity)
FirstName
LastName
Address (entity)
Contacts[] (entity) - this is an array of contacts.
Contact (entity)
Address (entity)


I'm transferring data from one system to another. I have a recursive
function set up to read from the (in this case the customer table) create
an
instance of the object, assign values to the properties, then create the
array and assign subentities to that and then assign the array to the
parent
entity's property. The theory works fine. As I said, all the data is
being
read from a table so I have no way of knowing what entities are needed
ahead
of time. This works when I only need one instance of an entity (like the
Customer's Address entity) using

string sEntityName = "Contact"
Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

I've tried your suggestions of using an ArrayList and
Array.CreateInstance.
These work so I can assign objects into them, but when it comes time to
add
the array or list to the Customer entity, it fails because the Custoemr
entity is expecting an array of Contacts, not a generic Array or
ArrayList.
I can't cast either because casting requires a known type. I've tried
(t.GetType()) and (typeof(t)) without success.
The way that the arrays are created with Jason's original code creates
the right type of array at runtime. Presumably you're using reflection
to set the property anyway, so it should be fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jason Newell

I forgot to mention that you can also strongly type the array in my
previous example.

string[] objList = (string[])arrayList.ToArray(typeof(string));

Jason Newell
Software Engineer
www.jasonnewell.net

Jason said:
Matt,

I think non of us still get what you're asking for. I would stab my
eyes out if I had to work on the code in your last reply. I have a hard
time believing that there is not a more eloquent way to write the code,
but then again, I don't understand what you're after.

Surely you can come up with something better with this example.

Type t = typeof(string); // Or whatever
ArrayList arrayList = new ArrayList(Array.CreateInstance(t, 0));
arrayList.Add("Hello");
arrayList.Add("World");
object[] objList = arrayList.ToArray();
objList[0] = "Goodbye";

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew said:
I got it. The problem was in the type declaration. I also had to use
the .GetConstructor method. The type with an array is idfferent thatn

Type t = Type.GetType(stype) + "[]");
object objList = t.GetContructor(new Type[](typeof(int32))).invoke(new
Object[] (...element count...));

You can't assign to objList[0], but you can create a new object{]
objListNew and cast it to an array of objects. Then I can still
assign objList to the main entity's property that is expecting an
array of type t.



Jon Skeet said:
I'm reading metadata I've netered into an Access table that defines
objects.
Each object can have primitive property types like string, int, etc,
but can
also have other objects defined as properties.. Here is one brief
example.

Customer (entity)
FirstName
LastName
Address (entity)
Contacts[] (entity) - this is an array of contacts.
Contact (entity)
Address (entity)


I'm transferring data from one system to another. I have a recursive
function set up to read from the (in this case the customer table)
create an
instance of the object, assign values to the properties, then create
the
array and assign subentities to that and then assign the array to
the parent
entity's property. The theory works fine. As I said, all the data
is being
read from a table so I have no way of knowing what entities are
needed ahead
of time. This works when I only need one instance of an entity (like
the
Customer's Address entity) using

string sEntityName = "Contact"
Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

I've tried your suggestions of using an ArrayList and
Array.CreateInstance.
These work so I can assign objects into them, but when it comes time
to add
the array or list to the Customer entity, it fails because the Custoemr
entity is expecting an array of Contacts, not a generic Array or
ArrayList.
I can't cast either because casting requires a known type. I've tried
(t.GetType()) and (typeof(t)) without success.
The way that the arrays are created with Jason's original code creates
the right type of array at runtime. Presumably you're using reflection
to set the property anyway, so it should be fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
M

Matthew Wells

I did find a way. The original problem was that I wanted an array of a
specific type, but I did not know the type until the statement was
executed.. I had previously figured out how to create a single instance:

string sMyType = "CustomerObject";
Type t = Type.GetType(sMyType);
Object obj = Activator.CreateInstance(t);

Like I said, this worked fine until I was trying to create an array of the
type. I was using the line

Type t = Type.GetType(sMyType);

and then trying to create an array of that like

Object[] objList = Activator.CreateInstance(t);

This didn't work because I was still working with a type of a single
instance of sMyType. I changed it to

Type t = Type.GetType(sMyType + "[]");

Now the original line for CreateInstance would not work because it was
trying to create an object using the default constructor with no parameters,
but the t[] type' constructor takes a parameter so I changed that line to:

Object[] objList = Activator.CreateInstance(t, new Object param[5]); \\ for
five elements.

(I think that was the syntax, I'm at home now and am not looking at the
code, but you get the idea). The only problem here is that you can't assign
directly into objList like

objList[0] = "abc"; \\ this creates an error that I still don't fully
understand.

But you can create a new array, cast it and assign objList to it, and then
when you're done assigning the elements you can still refer to objList since
it's referring to the same object.

Object[] objNewList = (Object) objList[];

So now it's much cleaner. What do you think?





Jon Skeet said:
Matthew Wells said:
I got it. The problem was in the type declaration. I also had to use
the
.GetConstructor method. The type with an array is idfferent thatn

Type t = Type.GetType(stype) + "[]");
object objList = t.GetContructor(new Type[](typeof(int32))).invoke(new
Object[] (...element count...));

You can't assign to objList[0], but you can create a new object{]
objListNew
and cast it to an array of objects. Then I can still assign objList to
the
main entity's property that is expecting an array of type t.

What does that do that using Array.CreateInstance doesn't do rather
more simply?
 
M

Matthew Wells

I did find a way. The original problem was that I wanted an array of a
specific type, but I did not know the type until the statement was
executed.. I had previously figured out how to create a single instance:

string sMyType = "CustomerObject";
Type t = Type.GetType(sMyType);
Object obj = Activator.CreateInstance(t);

Like I said, this worked fine until I was trying to create an array of the
type. I was using the line

Type t = Type.GetType(sMyType);

and then trying to create an array of that like

Object[] objList = Activator.CreateInstance(t);

This didn't work because I was still working with a type of a single
instance of sMyType. I changed it to

Type t = Type.GetType(sMyType + "[]");

Now the original line for CreateInstance would not work because it was
trying to create an object using the default constructor with no parameters,
but the t[] type' constructor takes a parameter so I changed that line to:

Object[] objList = Activator.CreateInstance(t, new Object param[5]); \\ for
five elements.

(I think that was the syntax, I'm at home now and am not looking at the
code, but you get the idea). The only problem here is that you can't assign
directly into objList like

objList[0] = "abc"; \\ this creates an error that I still don't fully
understand.

But you can create a new array, cast it and assign objList to it, and then
when you're done assigning the elements you can still refer to objList since
it's referring to the same object.

Object[] objNewList = (Object) objList[];

So now it's much cleaner. What do you think?
Jason Newell said:
Matt,

I think non of us still get what you're asking for. I would stab my eyes
out if I had to work on the code in your last reply. I have a hard time
believing that there is not a more eloquent way to write the code, but
then again, I don't understand what you're after.

Surely you can come up with something better with this example.

Type t = typeof(string); // Or whatever
ArrayList arrayList = new ArrayList(Array.CreateInstance(t, 0));
arrayList.Add("Hello");
arrayList.Add("World");
object[] objList = arrayList.ToArray();
objList[0] = "Goodbye";

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew said:
I got it. The problem was in the type declaration. I also had to use
the .GetConstructor method. The type with an array is idfferent thatn

Type t = Type.GetType(stype) + "[]");
object objList = t.GetContructor(new Type[](typeof(int32))).invoke(new
Object[] (...element count...));

You can't assign to objList[0], but you can create a new object{]
objListNew and cast it to an array of objects. Then I can still assign
objList to the main entity's property that is expecting an array of type
t.



Jon Skeet said:
I'm reading metadata I've netered into an Access table that defines
objects.
Each object can have primitive property types like string, int, etc,
but can
also have other objects defined as properties.. Here is one brief
example.

Customer (entity)
FirstName
LastName
Address (entity)
Contacts[] (entity) - this is an array of contacts.
Contact (entity)
Address (entity)


I'm transferring data from one system to another. I have a recursive
function set up to read from the (in this case the customer table)
create an
instance of the object, assign values to the properties, then create
the
array and assign subentities to that and then assign the array to the
parent
entity's property. The theory works fine. As I said, all the data is
being
read from a table so I have no way of knowing what entities are needed
ahead
of time. This works when I only need one instance of an entity (like
the
Customer's Address entity) using

string sEntityName = "Contact"
Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

I've tried your suggestions of using an ArrayList and
Array.CreateInstance.
These work so I can assign objects into them, but when it comes time to
add
the array or list to the Customer entity, it fails because the Custoemr
entity is expecting an array of Contacts, not a generic Array or
ArrayList.
I can't cast either because casting requires a known type. I've tried
(t.GetType()) and (typeof(t)) without success.
The way that the arrays are created with Jason's original code creates
the right type of array at runtime. Presumably you're using reflection
to set the property anyway, so it should be fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
M

Matthew Wells

Remember that I can't cast because I don't know the type ahead of time.


Jason Newell said:
I forgot to mention that you can also strongly type the array in my
previous example.

string[] objList = (string[])arrayList.ToArray(typeof(string));

Jason Newell
Software Engineer
www.jasonnewell.net

Jason said:
Matt,

I think non of us still get what you're asking for. I would stab my eyes
out if I had to work on the code in your last reply. I have a hard time
believing that there is not a more eloquent way to write the code, but
then again, I don't understand what you're after.

Surely you can come up with something better with this example.

Type t = typeof(string); // Or whatever
ArrayList arrayList = new ArrayList(Array.CreateInstance(t, 0));
arrayList.Add("Hello");
arrayList.Add("World");
object[] objList = arrayList.ToArray();
objList[0] = "Goodbye";

Jason Newell
Software Engineer
www.jasonnewell.net

Matthew said:
I got it. The problem was in the type declaration. I also had to use
the .GetConstructor method. The type with an array is idfferent thatn

Type t = Type.GetType(stype) + "[]");
object objList = t.GetContructor(new Type[](typeof(int32))).invoke(new
Object[] (...element count...));

You can't assign to objList[0], but you can create a new object{]
objListNew and cast it to an array of objects. Then I can still assign
objList to the main entity's property that is expecting an array of type
t.



I'm reading metadata I've netered into an Access table that defines
objects.
Each object can have primitive property types like string, int, etc,
but can
also have other objects defined as properties.. Here is one brief
example.

Customer (entity)
FirstName
LastName
Address (entity)
Contacts[] (entity) - this is an array of contacts.
Contact (entity)
Address (entity)


I'm transferring data from one system to another. I have a recursive
function set up to read from the (in this case the customer table)
create an
instance of the object, assign values to the properties, then create
the
array and assign subentities to that and then assign the array to the
parent
entity's property. The theory works fine. As I said, all the data is
being
read from a table so I have no way of knowing what entities are needed
ahead
of time. This works when I only need one instance of an entity (like
the
Customer's Address entity) using

string sEntityName = "Contact"
Type t = Type.GetType(sEntityName);
Object = objNew = Activator.CreateInstance(t);

I've tried your suggestions of using an ArrayList and
Array.CreateInstance.
These work so I can assign objects into them, but when it comes time
to add
the array or list to the Customer entity, it fails because the
Custoemr
entity is expecting an array of Contacts, not a generic Array or
ArrayList.
I can't cast either because casting requires a known type. I've tried
(t.GetType()) and (typeof(t)) without success.
The way that the arrays are created with Jason's original code creates
the right type of array at runtime. Presumably you're using reflection
to set the property anyway, so it should be fine.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jon Skeet [C# MVP]

Matthew Wells said:
I did find a way.

Yes, involving this:

Type t = Type.GetType(sMyType + "[]");

and then using Activator.CreateInstance.

However, Array.CreateInstance had previously been suggested, and I
believe that would solve your problem more simply. What does it not do
that you needed it to do?
 
M

Matthew Wells

I did say that's what I'm using (this is a little corrected from my last
post - I checked the actual line)

Object objList = Activator.CreateInstance(t, new Object[1] {5}); \\ for five
elements

You'll notice that I'm now using the signature for CreatteInstance that is
needed for an array of type t.

But like I said before, if you use this, you can't assign a value to an
element of the array like:

objList[0] = 2;

You'll get an error. You have to declare another array, cast it, and then
assign this array to that.

Object[] ObjListNew = (Object[])objList;

After you're done using ObjListNew, you can jsut refer to the original
objList since they refer to the same object.

Nice and neat.


Jon Skeet said:
Matthew Wells said:
I did find a way.

Yes, involving this:

Type t = Type.GetType(sMyType + "[]");

and then using Activator.CreateInstance.

However, Array.CreateInstance had previously been suggested, and I
believe that would solve your problem more simply. What does it not do
that you needed it to do?
 
J

Jon Skeet [C# MVP]

Matthew Wells said:
I did say that's what I'm using (this is a little corrected from my last
post - I checked the actual line)

Object objList = Activator.CreateInstance(t, new Object[1] {5}); \\ for five
elements

You'll notice that I'm now using the signature for CreatteInstance that is
needed for an array of type t.

But you're still using *Activator*.CreateInstance rather than
*Array*.CreateInstance.

The call to Array.CreateInstance is simpler and more obviously
understandable, IMO.
But like I said before, if you use this, you can't assign a value to an
element of the array like:

objList[0] = 2;

You'll get an error. You have to declare another array, cast it, and then
assign this array to that.

Object[] ObjListNew = (Object[])objList;

After you're done using ObjListNew, you can jsut refer to the original
objList since they refer to the same object.

Nice and neat.

All of that is still true with Array.CreateInstance, but you don't have
to go through as many hoops.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top