none
TypeBuilder.DefineMethod generates a different IL code for string[,] in .net 3.5 than in .net 4.0 RRS feed

  • Question

  • I have an external exe calling into a dll, which is generated using Reflection. Until now, I had been using .net 2.0 to dynamically generate the assembly and the application calling into this library was written in .net 4.0. Now when I use .net 4.0 to generate the assembly using reflection, the original exe no longer works!!

    I get a System.MissingMethodException on one of the methods exposed in the dll. This methods just takes in a 2d array of strings. (string [,]).

    Below are the IL method signatures which was generated in two different cases.

    In .net 2.0

    .method public static 
        void Test (
            string[, ] ArrayRef
        ) cil managed 

    and in .net 4.0

    .method public static 
        void Test (
            string[0..., 0...] ArrayRef
        ) cil managed 

    Now my questions

    1. Why has the TypeBuilder.DefineMethod generated different signature for the same method? 

    2. What changes were introduced in Reflection.Emit to cause these changes?

    3. Is string[0..., 0...] and string[, ] same?  (From the exception I have got, they don't seem to cast very well, and hence the exception). If not, what are the differences? ( I tried searching the documentation for [0...,0...] notation on msdn documentation, but couldn't find anything)

    4. Is there anyway to generate parameters with string[, ] format in .net 4.0, so as to provide backward compatibility? 

    Tuesday, December 11, 2012 12:03 PM

Answers

  • 1. Because it was broken in 2.0, it should have generated string[0...,0...], not string[,].

    2. The bug mentioned above was fixed.

    3. They aren't, they really are different types. [,] means that the lower bound is not specified, [0...,0...] means that the lower bound is 0. The IL equivalent of C#'s string[,] is really string[0...,0...], not string[,].

    4. Not that I know of, it seems that now it always prevents arrays of type [,]. You really should update the exe to work with string[0...,0...], cotinuing to use [,] is kind of asking for trouble.

    • Marked as answer by Sridarshan Wednesday, December 12, 2012 5:17 AM
    Tuesday, December 11, 2012 2:32 PM
    Moderator
  • "Has this bug been fixed only in .net 4.0 or has there been any patch released for .net 2.0 - .net3.5? "

    Well, the bug can be reproduced with an up to date installation of .net 2.0/3.5 so obviously it has not been fixed for 2.0.

    I did a search and found this bug which has the same root cause and they don't mention a fix being available:

    http://support.microsoft.com/kb/960240

    • Marked as answer by Sridarshan Wednesday, December 12, 2012 5:17 AM
    Tuesday, December 11, 2012 3:08 PM
    Moderator

All replies

  • Can you not simply use the overload method that explicitly defines the method signature?

    http://msdn.microsoft.com/en-us/library/cbwdt101(v=vs.100).aspx


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    Tuesday, December 11, 2012 12:24 PM
  • I am indeed using the one you pointed me to. I haven't changed anything in the assembly builder projects which builds this assembly.
    Tuesday, December 11, 2012 12:35 PM
  • Can you identify which release of the framework made the breaking change? 3.0, 3.5, or 4.0

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.


    Tuesday, December 11, 2012 1:23 PM
  • Can you identify which release of the framework made the breaking change? 3.0, 3.5, or 4.0

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.


    The breaking change has been caused by .net 4.0 framework. 
    Tuesday, December 11, 2012 1:30 PM
  • 1. Because it was broken in 2.0, it should have generated string[0...,0...], not string[,].

    2. The bug mentioned above was fixed.

    3. They aren't, they really are different types. [,] means that the lower bound is not specified, [0...,0...] means that the lower bound is 0. The IL equivalent of C#'s string[,] is really string[0...,0...], not string[,].

    4. Not that I know of, it seems that now it always prevents arrays of type [,]. You really should update the exe to work with string[0...,0...], cotinuing to use [,] is kind of asking for trouble.

    • Marked as answer by Sridarshan Wednesday, December 12, 2012 5:17 AM
    Tuesday, December 11, 2012 2:32 PM
    Moderator
  • 1. Because it was broken in 2.0, it should have generated string[0...,0...], not string[,].

    2. The bug mentioned above was fixed.

    3. They aren't, they really are different types. [,] means that the lower bound is not specified, [0...,0...] means that the lower bound is 0. The IL equivalent of C#'s string[,] is really string[0...,0...], not string[,].

    4. Not that I know of, it seems that now it always prevents arrays of type [,]. You really should update the exe to work with string[0...,0...], cotinuing to use [,] is kind of asking for trouble.

    Has this bug been fixed only in .net 4.0 or has there been any patch released for .net 2.0 - .net3.5? 

    Thanks a lot for taking your time to answer the question. 

    • Marked as answer by Sridarshan Wednesday, December 12, 2012 5:17 AM
    • Unmarked as answer by Sridarshan Wednesday, December 12, 2012 5:17 AM
    Tuesday, December 11, 2012 2:52 PM
  • "Has this bug been fixed only in .net 4.0 or has there been any patch released for .net 2.0 - .net3.5? "

    Well, the bug can be reproduced with an up to date installation of .net 2.0/3.5 so obviously it has not been fixed for 2.0.

    I did a search and found this bug which has the same root cause and they don't mention a fix being available:

    http://support.microsoft.com/kb/960240

    • Marked as answer by Sridarshan Wednesday, December 12, 2012 5:17 AM
    Tuesday, December 11, 2012 3:08 PM
    Moderator