PRB: .NET Multidimensional arrays emitted by Reflection do not match compiled arrays

Article ID: 960240 – Last Review: November 18, 2008 – Revision: 1.0

PRB: .NET Multidimensional arrays emitted by Reflection do not match compiled arrays

Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Action

  1. Use the System.Reflection.Emit.FieldBuilder class to create and emit a field with a multidimensional array type and save an assembly with the AssemblyBuilder.  For instance, you may have some code like the following:
    AssemblyName an = new AssemblyName(ClassLibrary1);
    AssemblyBuilder assb = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder modb = assb.DefineDynamicModule(ClassLibrary1, ClassLibrary1.dll, false);
    TypeBuilder tb = modb.DefineType(ClassLibrary1.Class1, TypeAttributes.Class | TypeAttributes.Public);
    ConstructorBuilder cb = tb.DefineDefaultConstructor(MethodAttributes.Public);
    FieldBuilder fb = tb.DefineField(ia, typeof(int).MakeArrayType(2), FieldAttributes.Public | FieldAttributes.Static);
    Type t1 = tb.CreateType();
    assb.Save(ClassLibrary1.dll);
  2. Use the C# compiler to compile what should be equivalent code.  Something like this, perhaps:
    namespace ClassLibrary1
    {
        public class Class1
        {
            public static int[,] ia;
            public Class1()
            {
            } // Class1()
        } // class Class1
    } // namespace ClassLibrary1
  3. Create a C# application with a reference to the compiled ClassLibrary1, and add some code like the following:
    ClassLibrary1.Class1.i2 = new int[2, 3];

  4. Run the code from step 1, emitting a new ClassLibrary1.dll.

  5. Physically overwrite the ClassLibrary1.dll assembly generated by the compiler in step 2 with the ClassLibrary1.dll emitted in step 4.

  6. Run the program you created in step 3. 

Result

Your new application will encounter a MissingFieldException.

Cause

Use ILDAsm to examine the generated MSIL code for each of the assemblies from steps 2 and 4.

You will observe that in the emitted code, the data type of the array field in IL is int32[,], but for the compiled assembly, the type of the array field in IL is int32[0…,0…].

The two representations are functionally equivalent ways to express the two-dimensional array field with zero lower bounds. But if you have another assembly that was built with a reference to one of these assemblies, and try to replace it with the other, you will encounter a MissingFieldException.

Although the two fields are functionally equivalent, the compiler changes the field info string enough that the .NET Runtime thinks it hasn’t found a matching type, and cannot load the field.  Thus, a MissingFieldException is thrown.  Reflection.Emit generates a different signature than the C# compiler and the type loader rejects it because the two signatures don’t match.  Note that C# was used in the example, but the same difference will be observed if VB.NET or another .NET language compiler is used.

Resolution

Avoid using multidimensional arrays in classes where this anomaly might be a problem for you.

Microsoft has recognized this problem, and it is currently planned that a future release of the .NET Framework will include a System.Reflection.Emit namespace that will have corrected this.

More Information

The same problem will be observed if you try to emit overrides of inherited members of compiled types that use multidimensional array types, or if you try to emit implementations of compiled interface members that use multidimensional array types.

See also:
System.Reflection.Emit Namespace (http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx)
MSIL Disassembler (Ildasm.exe) (http://msdn.microsoft.com/en-us/library/f7dy01k1.aspx)

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.


APPLIES TO
  • Microsoft .NET Framework Class Libraries 2.0
  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 2.0 Service Pack 1 (x86)
  • Microsoft .NET Framework 3.0
  • Microsoft .NET Framework 3.5
  • Microsoft .NET Framework 3.5 Service Pack 1
Keywords: 
kbnomt kbrapidpub KB960240

 

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image