c# - Unable to cast List<int[*]> to List<int[]> instantiated with reflection -


i instantiating list<t> of single dimensional int32 arrays reflection. when instantiate list using:

type typeint = typeof(system.int32); type typeintarray = typeint.makearraytype(1); type typelistgeneric = typeof(system.collections.generic.list<>); type typelist = typelistgeneric.makegenerictype(new type[] { typeintarray, });  object instance = typelist.getconstructor(type.emptytypes).invoke(null); 

i see strange behavior on list itself:

watch of list instance object

if interface through reflection seems behave normally, if try cast actual type:

list<int[]> list = (list<int[]>)instance; 

i exception:

unable cast object of type 'system.collections.generic.list`1[system.int32[*]]' type 'system.collections.generic.list`1[system.int32[]]'.

any ideas may causing or how resolve it? working in visual studio 2010 express on .net 4.0.

the problem caused makearraytype function. way you're using create multidimensional array 1 dimension, not same 1 dimensional array (vector).

from documentation:

the common language runtime makes distinction between vectors (that is, one-dimensional arrays zero-based) , multidimensional arrays. vector, has 1 dimension, not same multidimensional array happens have 1 dimension. cannot use method overload create vector type; if rank 1, method overload returns multidimensional array type happens have 1 dimension. use makearraytype() method overload create vector types.

change:

type typeintarray = typeint.makearraytype(1); 

to this:

type typeintarray = typeint.makearraytype(); 

to create ordinary one-dimensional vector.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -