Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
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 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.
- 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");
- 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
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];
Run the code from step 1, emitting a new ClassLibrary1.dll.
Physically overwrite the ClassLibrary1.dll assembly generated by the compiler in step 2Â with the ClassLibrary1.dll emitted in step 4.
Run the program you created in step 3.Â
Your new application will encounter a MissingFieldException.
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.
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.
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)
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
| kbnomt kbrapidpub KB960240 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please