Activator.CreateInstance for new Array Element. Type Problem

G

garak

Hi,

I got the following problem :

I have an defined an Array of different Actions:
public PRootActions[] AllActions = new PRootActions [10];

I got a dynamic method where all Actions or other things like
Materials are loaded from a Database to my Actions or Materials
Array:
public void All_Db2Cl (Object[] AllData, ref int totalData)

This method is called like this:
PMain.myPArray.All_Db2Cl (AllAction, ref
PMain.myPGlobals.totalActions);

Inside this Method I call the Activator.CreateInstance Method to
initialize the Array Element:
AllData = Ativator.CreateInstance (null,
"PentaEngine.PRootActions");

With this instruction I receive the following error message :

An unhandled exception of type 'System.ArrayTypeMismatchException'
occurred in PentaEngine.exe

Additional information: Attempted to store an element of the incorrect
type into the array.


Does anyone has an idea ?

Thank you very much
Frank
 
R

Robert Jordan

Hi,
Inside this Method I call the Activator.CreateInstance Method to
initialize the Array Element:
AllData = Ativator.CreateInstance (null,
"PentaEngine.PRootActions");

With this instruction I receive the following error message :

An unhandled exception of type 'System.ArrayTypeMismatchException'
occurred in PentaEngine.exe

Arrays must be created using System.Array.CreateInstance.

Bye
Rob
 
J

Jon Skeet [C# MVP]

garak said:
I got the following problem :

I have an defined an Array of different Actions:
public PRootActions[] AllActions = new PRootActions [10];

I got a dynamic method where all Actions or other things like
Materials are loaded from a Database to my Actions or Materials
Array:
public void All_Db2Cl (Object[] AllData, ref int totalData)

This method is called like this:
PMain.myPArray.All_Db2Cl (AllAction, ref
PMain.myPGlobals.totalActions);

Inside this Method I call the Activator.CreateInstance Method to
initialize the Array Element:
AllData = Ativator.CreateInstance (null,
"PentaEngine.PRootActions");

With this instruction I receive the following error message :

An unhandled exception of type 'System.ArrayTypeMismatchException'
occurred in PentaEngine.exe

Additional information: Attempted to store an element of the incorrect
type into the array.

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.
 
G

garak

OK Thanks,
but with Array.CreateInstance I need to have a TYPE variable,
describing the desired type. I only have the typename as a STRING
variable.
Is there a way to get a TYPE variable with the typename ?
 
J

Jon Skeet [C# MVP]

garak said:
OK Thanks,
but with Array.CreateInstance I need to have a TYPE variable,
describing the desired type. I only have the typename as a STRING
variable.
Is there a way to get a TYPE variable with the typename ?

Type.GetType.
 

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