mardi 2 mai 2017

Cast type which i don't know yet what is it

let's say i have list with properties like this:

Type ArrayType
String Value 

i've got another list like this:

ArrayType=System.Int32
Value=44
Index=0

ArrayType=System.Int32
Value=11
Index=3

ArrayType=System.Int32
Value=7
Index=2

ArrayType=System.Int32
Value=5
Index=1

All values are in string ! I don't know what type will be it, int32 is only example. there will also be floats, strings, etc.

so... i'm creating new array of type which i readed from "ArrayType" property

dynamic newArray = Array.CreateInstance(typeOfArray, arraySize);

I know arraySize of coure

and the Arrays are created properly. But now i want to copy all values (which are in string) to my new array: but i have no idea how to cast it. i've tried something like this:

newArray.SetValue(element.Property.Value,element.Index);

it throw an exception that he can't write OBJECT to my array of ints

so then i've tried to cast in somehow:

 newArray[element.Index] = Convert.ChangeType(element.Property.Value,element.Property.Type); 

but it still can't cast the object

can someone help :) ?





Aucun commentaire:

Enregistrer un commentaire