Hi all,

I have some code which has been using the CCI Metadata library, but
I've just rewritten it to use the latest version of Mono.Cecil (from
source). When I run my code (which modifies an assembly), the modified
assembly runs through peverify without any errors/warnings, but when I
run the assembly, it throws an ArrayTypeMismatchException.

To give an overview of my code: the part in question creates a new
object[]-typed local variable in an existing method, then emits a
Newarr and Stloc to create the array and store it in the variable (and
all of that works just fine). However, later on in the method, I add
some instructions to store an item into the array:

Ldloc    ; to load the object[] array reference
Ldc_i4_0    ; load the index which we'll store the item at
Ldloc    ; load the item we want to store into the array; the variable
in question is a reference type, so we don't need to box it
Stelem.ref    ; store the item into the array

The ArrayTypeMismatchException is thrown when the stelem.ref occurs.
Does anyone have a guess as to why this might be happening? As I said
before, there are no validation errors from peverify. The IL (in the
rewritten method body) is identical to what was produced by the CCI-
based version of my code, so I know that there aren't any issues
there.

The only thing I can think of is that there is some issue with how
Cecil is saving the metadata for the local variable, such that it
doesn't appear to be an object[] array to the CLR, and so the
Stelem.Ref instruction is throwing the exception thinking that the
item (again, a reference type) isn't derived from the element type of
the array (i.e., the Object type it's using doesn't match the Object
type in the CLR). If it matters, I'm using the .NET 4.0 version of
Cecil (since that's what my tool is based on), but the assembly I'm
modifying is a .NET 2.0 assembly; and yes, I've made sure to import
the correct version of Object -- the modified assembly still correctly
references mscorlib 2.0 (and not 4.0).

Any help would be much appreciated.

Best Regards,
Jack

-- 
--
mono-cecil

Reply via email to