Activator.CreateInstance Type problem

  • Thread starter Thread starter Frank Pleyer via .NET 247
  • Start date Start date
F

Frank Pleyer via .NET 247

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.

When I write this instead everthing runs finde (but I need to take the first option :( )
AllData = Ativator.CreateInstance ((AllData.GetType()).GetElementType());


Does anyone have an idea ?

Thank you very much
Frank
 
Hi Frank,

Indeed, the first method returns an ObjectHandle, and the second returns an object.
Any array can store objects, but to be able to store ObjectHandles the array must be declared as an ObjectHandle array or any parent class of ObjectHandle

Check the AllData declaration which should be

ObjectHandle[] AllData = new ObjectHandle[some number]

If AllData can store several things, consider an ArrayList or declare the array as an object[]

object[] AllData = new object[some number]

The error occurs when you attempt to store an ObjectHandle into an array that can't accept it.
 
Actually, upon rereading I see that AllData is indeed declared as an Object[], so this would be fine. However, is Allaction and object array, or ObjectHandle array.
 
Back
Top