Cast elements on an array of unknown types by using reflection in C# -


i have dictionary object keys of type string , values of type object because extracted file data type (for column) might differ.

dictionary<string, object[]> lfilecontent; 

i type of every array , casting type. of course cannot while in dictionary when extract each value use them. instance (i use pseudocode c# using logical approach):

ltype = lfilecontent["key1"].value.gettype(); ltype newarray = (ltype) filecontent["key1"].value; 

i make 3 questions:

1) when elements file , store them object, keep information reflection uses type?

2) if (1) not apply, shall use reflection type when extract them file (before inserting object in dictionary)?

3) how can make such casting using reflection?

thanks

francesco

you don't need reflection, object retain type info. cast object type have tell type @ compile time, use reflection dynamically manipulate object, that's different casting type. use this:

string[] stringarray = filecontent["key1"].value string[]; if (stringarray != null) {     // object string[] type, safe use stringarray variable }  // , continue other types may stored in dictionary 

Comments