Hi Jb, I've just done a bit more digging, and before the assembly is modified, there aren't any entries in the TypeSpec table. After rewriting the assembly with Cecil, there is one entry, which looks like it's for the object[] type I created for the local variable (via the ArrayType constructor). However, a local variable of type object[] shouldn't need a TypeSpec entry, it should just store the type as part of the LocalVarSig for the variable (see section 23.2.12, Partition II of the ECMA-335 spec - which is on pg. 286 / 556 in my PDF version). Perhaps when the AssemblyWriter is saving the assembly, it does a comparison of the element type (System.Object, mscorlib 2.0) against typeof(object) (which would return System.Object, mscorlib 4.0, since my rewriting app is .NET 4.0-based), which fails, so then Cecil creates a TypeSpec instead of just encoding the type as part of the LocalVarSig?
I'll try to put a repro together for you and send it along shortly. Best Regards, Jack On Jan 30, 2:30 pm, Jb Evain <[email protected]> wrote: > Hi, > > Well, this doesn't ring any bell of a known issue, and it sounds a bit > obscure. Please provide me with some way to reproduce this. > > Thanks! > > On Sunday, January 30, 2011, Jack Pappas <[email protected]> wrote: > > 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 > > -- -- mono-cecil
