Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6967657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:21:01+00:00 2026-05-27T16:21:01+00:00

First I must aplogise for being a noob with IL. I am having difficulty

  • 0

First I must aplogise for being a noob with IL.
I am having difficulty generating IL code to call a method with this signature:

public void CallMethod2(string name, object[] args, object[] genericArgs)

I am able to call a method that has a single array that looks like this:

public void CallMethod1(string name, object[] args)

using the following IL works:

ILGenerator ilgen = myMethod.GetILGenerator();
var il = ilgen;
MethodInfo invokerMethod = typeof(Proxy<T>).GetMethod("CallMethod1", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldstr, method.Name);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Newarr, typeof(System.Object));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Ldarg, 1);
il.Emit(OpCodes.Stelem_Ref);
il.Emit(OpCodes.Ldloc_0);

il.Emit(OpCodes.Call, invokerMethod);
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ret);

But then I use the following IL to try and call CallMethod2 using this IL:

ILGenerator ilgen = myMethod.GetILGenerator();
var il = ilgen;
MethodInfo invokerMethod = typeof(Proxy<T>).GetMethod("CallMethod2", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldstr, method.Name);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Newarr, typeof(System.Object));
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Ldarg, 1);
il.Emit(OpCodes.Stelem_Ref);

il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Newarr, typeof(System.Object));
il.Emit(OpCodes.Stloc_1);
il.Emit(OpCodes.Ldloc_1);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Ldarg, 1);
il.Emit(OpCodes.Stelem_Ref);

il.Emit(OpCodes.Ldloc_1);
il.Emit(OpCodes.Ldloc_2);

il.Emit(OpCodes.Call, invokerMethod);
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ret);

This IL with additional object[] I get an error:

Common Language Runtime detected an invalid program.

As you can see all i did was added the 2nd block to populate the array and call the method, it seems that by using StLoc_1 it just corrupts it.

I wrote the same method and called it normally and looked at ILDasm and the codes seem to all tie up.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:21:01+00:00Added an answer on May 27, 2026 at 4:21 pm

    I’m very confused… you see: that code shouldn’t work since you haven’t actually allocated any locals; for example, here’s a badly written (in that it uses unnecessary locals) multiply-by-4 method, that doesn’t declare the locals:

        var method = new DynamicMethod("MulBy4", typeof (int),
             new Type[] {typeof (int)});
        var il = method.GetILGenerator();
        il.Emit(OpCodes.Ldc_I4_4);
        il.Emit(OpCodes.Stloc_0); // this usage is 
        il.Emit(OpCodes.Ldloc_0); // deliberately silly
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Mul);
        il.Emit(OpCodes.Stloc_1); // this usage is 
        il.Emit(OpCodes.Ldloc_1); // deliberately silly
        il.Emit(OpCodes.Ret);
        var mulBy4= (Func<int,int>)method.CreateDelegate(typeof (Func<int, int>));
        var twelve = mulBy4(3);
    

    This creates the VerificationException:

    Operation could destabilize the runtime.

    since it is unverifiable. It is bad IL! If we change it to:

        var method = new DynamicMethod("MulBy4", typeof (int),
             new Type[] {typeof (int)});
        var il = method.GetILGenerator();
        il.DeclareLocal(typeof (int));
        il.DeclareLocal(typeof(int));
        ...
    

    then now it works. This then leads onto an alternative to remembering the numbers – by storing and using the LocalBuilder that is returned from DeclareLocal:

        var method = new DynamicMethod("MulBy4", typeof (int),
             new Type[] {typeof (int)});
        var il = method.GetILGenerator();
        var multiplier = il.DeclareLocal(typeof (int));
        var result = il.DeclareLocal(typeof(int));
        il.Emit(OpCodes.Ldc_I4_4);
        il.Emit(OpCodes.Stloc, multiplier);
        il.Emit(OpCodes.Ldloc, multiplier);
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Mul);
        il.Emit(OpCodes.Stloc, result);
        il.Emit(OpCodes.Ldloc, result);
        il.Emit(OpCodes.Ret);
        var mulBy4= (Func<int,int>)method.CreateDelegate(typeof (Func<int, int>));
        var twelve = mulBy4(3);
    

    If you are concerned that this uses the longer IL version, then you can use instead:

    static void LoadLocal(this ILGenerator il, LocalBuilder local)
    {
        switch(local.LocalIndex)
        {
            case 0: il.Emit(OpCodes.Ldloc_0); break;
            case 1: il.Emit(OpCodes.Ldloc_1); break;
            case 2: il.Emit(OpCodes.Ldloc_2); break;
            case 3: il.Emit(OpCodes.Ldloc_3); break;
            default:
                if(local.LocalIndex < 256)
                {
                    il.Emit(OpCodes.Ldloc_S, (byte) local.LocalIndex);
                } else
                {
                    il.Emit(OpCodes.Ldloc, (ushort) local.LocalIndex);
                }
                break;
        }
    }
    

    along with il.LoadLocal(multiplier); and il.LoadLocal(result); (and obviously something similar for Stloc)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, first of all sorry about this question it must be pretty straight forward
I'm using the following code to show a particular UITableView: the first row must
I have two method in service - GetToken and GetData. User must first execute
So, i have some block, and this block must contains two divs, first div
I must admit, this is my first post on this site, so I apologise
First I must say that I'm not good at English and completely new to
Google App Engine says Must authenticate first. while trying to deploy any app: me@myhost
I must first admit that I'm new to Analysis Services but now must extend
I need to write parser. And I must login there: http://24travels.pl/web/export.pl?o=10004&co=imprezy But this is
first i must assume that i'm not very familiar with the C# yield keyword

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.