mardi 27 septembre 2016

Get Type of instance via GetType(), then using it to initialize new instance

I have a fieldInfo, and I know it's an array so I use it's value to declare an Array instance, and also get the Type of the array elements:

void Test(object baseInstance, FieldInfo baseInstanceField) {
    Array a = (Array)baseInstanceField.GetValue(baseInstance);
    Type elementType = TypeSystem.GetElementType(baseInstance.GetType());

}

Now I want to initialize a new array of Type elementType with a specific length, how do I do this? I later will have to access the elements of this new array.

elementType[] newArray = new elementType[34];//doesn't work

The type or namespace name `elementType' could not be found. Are you missing a using directive or an assembly reference? P.S. I'm using Reflection, of course.





Aucun commentaire:

Enregistrer un commentaire