none
ILGenerator problems RRS feed

  • Question

  • I am developing my own language and am getting and BadImageFormatException Class Token.

    I think the problem lies in this set of code

    il.Emit(OpCodes.Ldc_I4, count); il.Emit(OpCodes.NewArray, typeof(object)); for(int I = 0; I != count; I++) { Tree child = tree[0][I]; il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldc_I4, I); EmitExpression(child, il); // should for the test I am using just result in an integer 1 boxed as an object il.Emit(OpCodes.Stelem);

    }

    The purpose of the code is create an object array of arguments that is then passed to a method of type object method(object[]). if the method does not have any arguments an object array of length zero then everything works, but if the method has args then it fails like for example args in the form new object[] { 1 }


    I am wondering if it is the dup part? I am using dup so that I do not have to store the array and can just keep it in memory, but maybe there is something I am missing about how things work
    • Edited by devgames Tuesday, November 21, 2017 1:39 AM
    Tuesday, November 21, 2017 1:36 AM

All replies

  • Hi devgames

    Thank you for posting here.

    According to your description, I could not test the code you provided to reproduce the error.

    You could download the source file from code project for reference. It talks about how to use ILGenerator.

    https://www.codeproject.com/Articles/14973/Dynamic-Code-Generation-vs-Reflection

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 21, 2017 7:18 AM
    Moderator
  • Maybe you should use OpCodes.Stelem_Ref.

    Tuesday, November 21, 2017 9:34 AM
  • Hi devgames,

    >>I am wondering if it is the dup part? I am using dup so that I do not have to store the array and can just keep it in memory, but maybe there is something I am missing about how things work

    If you are unsure of the type of variable, I would suggest that you could write c# code first, then generate IL code by using IL tools(such as ildasm.exe), and you could find related OpCodes in IL code.

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 29, 2017 9:41 AM
    Moderator