IL code to call method with out parameter












2














I want to IL generate a dynamic method



delegate ArraySegment<byte> X(MyClass mc);


that calls a method of the signature on mc and returns its out parameter.



MethodInfo methInf = aClass.GetMethod("Y",
BindingFlags.Public | BindingFlags.Instance,
null, new { typeof(ArraySegment<byte>).MakeByRefType() }, null);


but I don't know how to handle the out parameter. Here's the code I have so far.



DynamicMethod dm = new DynamicMethod("X", typeof(ArraySegment<byte>),
new { typeof(MyClass) });
ILGenerator il = dm.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Callvirt, methInf, null);


What's needed to make the out param work?










share|improve this question
























  • Just to clarify, your question is about how to emit a call to a method featuring an out parameter?
    – elgonzo
    Nov 12 '18 at 23:55










  • Since the parameter is already a ref type, you should just have to load the parameter (which is a reference) - whether that is arg0 or arg1 depends on instance vs static
    – Marc Gravell
    Nov 13 '18 at 0:05










  • ref and out are actually identical to the CLR. They only differ by how the compiler enforces that code sets an out before the method returns.
    – Crowcoder
    Nov 13 '18 at 0:39
















2














I want to IL generate a dynamic method



delegate ArraySegment<byte> X(MyClass mc);


that calls a method of the signature on mc and returns its out parameter.



MethodInfo methInf = aClass.GetMethod("Y",
BindingFlags.Public | BindingFlags.Instance,
null, new { typeof(ArraySegment<byte>).MakeByRefType() }, null);


but I don't know how to handle the out parameter. Here's the code I have so far.



DynamicMethod dm = new DynamicMethod("X", typeof(ArraySegment<byte>),
new { typeof(MyClass) });
ILGenerator il = dm.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Callvirt, methInf, null);


What's needed to make the out param work?










share|improve this question
























  • Just to clarify, your question is about how to emit a call to a method featuring an out parameter?
    – elgonzo
    Nov 12 '18 at 23:55










  • Since the parameter is already a ref type, you should just have to load the parameter (which is a reference) - whether that is arg0 or arg1 depends on instance vs static
    – Marc Gravell
    Nov 13 '18 at 0:05










  • ref and out are actually identical to the CLR. They only differ by how the compiler enforces that code sets an out before the method returns.
    – Crowcoder
    Nov 13 '18 at 0:39














2












2








2







I want to IL generate a dynamic method



delegate ArraySegment<byte> X(MyClass mc);


that calls a method of the signature on mc and returns its out parameter.



MethodInfo methInf = aClass.GetMethod("Y",
BindingFlags.Public | BindingFlags.Instance,
null, new { typeof(ArraySegment<byte>).MakeByRefType() }, null);


but I don't know how to handle the out parameter. Here's the code I have so far.



DynamicMethod dm = new DynamicMethod("X", typeof(ArraySegment<byte>),
new { typeof(MyClass) });
ILGenerator il = dm.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Callvirt, methInf, null);


What's needed to make the out param work?










share|improve this question















I want to IL generate a dynamic method



delegate ArraySegment<byte> X(MyClass mc);


that calls a method of the signature on mc and returns its out parameter.



MethodInfo methInf = aClass.GetMethod("Y",
BindingFlags.Public | BindingFlags.Instance,
null, new { typeof(ArraySegment<byte>).MakeByRefType() }, null);


but I don't know how to handle the out parameter. Here's the code I have so far.



DynamicMethod dm = new DynamicMethod("X", typeof(ArraySegment<byte>),
new { typeof(MyClass) });
ILGenerator il = dm.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Callvirt, methInf, null);


What's needed to make the out param work?







c# cil emit out-parameters ilgenerator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 0:56

























asked Nov 12 '18 at 23:48









Eugene Beresovsky

10.8k660110




10.8k660110












  • Just to clarify, your question is about how to emit a call to a method featuring an out parameter?
    – elgonzo
    Nov 12 '18 at 23:55










  • Since the parameter is already a ref type, you should just have to load the parameter (which is a reference) - whether that is arg0 or arg1 depends on instance vs static
    – Marc Gravell
    Nov 13 '18 at 0:05










  • ref and out are actually identical to the CLR. They only differ by how the compiler enforces that code sets an out before the method returns.
    – Crowcoder
    Nov 13 '18 at 0:39


















  • Just to clarify, your question is about how to emit a call to a method featuring an out parameter?
    – elgonzo
    Nov 12 '18 at 23:55










  • Since the parameter is already a ref type, you should just have to load the parameter (which is a reference) - whether that is arg0 or arg1 depends on instance vs static
    – Marc Gravell
    Nov 13 '18 at 0:05










  • ref and out are actually identical to the CLR. They only differ by how the compiler enforces that code sets an out before the method returns.
    – Crowcoder
    Nov 13 '18 at 0:39
















Just to clarify, your question is about how to emit a call to a method featuring an out parameter?
– elgonzo
Nov 12 '18 at 23:55




Just to clarify, your question is about how to emit a call to a method featuring an out parameter?
– elgonzo
Nov 12 '18 at 23:55












Since the parameter is already a ref type, you should just have to load the parameter (which is a reference) - whether that is arg0 or arg1 depends on instance vs static
– Marc Gravell
Nov 13 '18 at 0:05




Since the parameter is already a ref type, you should just have to load the parameter (which is a reference) - whether that is arg0 or arg1 depends on instance vs static
– Marc Gravell
Nov 13 '18 at 0:05












ref and out are actually identical to the CLR. They only differ by how the compiler enforces that code sets an out before the method returns.
– Crowcoder
Nov 13 '18 at 0:39




ref and out are actually identical to the CLR. They only differ by how the compiler enforces that code sets an out before the method returns.
– Crowcoder
Nov 13 '18 at 0:39












1 Answer
1






active

oldest

votes


















0














Thanks @MarcGravell (also for your deleted answer, which was of great help, as it spells out what you hint at in your comment to my question) and ILSpy, which helped me by compiling c# code to CIL, so I could just peek at that.



So here's the final, working code:



LocalBuilder local = il.DeclareLocal(typeof(ArraySegment<byte>));
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloca, local);
il.EmitCall(OpCodes.Callvirt, methInf, null);
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Ldloc, local.LocalIndex);
il.Emit(OpCodes.Ret);


Invoking this is 10 times faster than doing methodInfo.Invoke(...) (on a methodInfo object that was created only once, of course).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271752%2fil-code-to-call-method-with-out-parameter%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks @MarcGravell (also for your deleted answer, which was of great help, as it spells out what you hint at in your comment to my question) and ILSpy, which helped me by compiling c# code to CIL, so I could just peek at that.



    So here's the final, working code:



    LocalBuilder local = il.DeclareLocal(typeof(ArraySegment<byte>));
    il.Emit(OpCodes.Ldarg_0);
    il.Emit(OpCodes.Ldloca, local);
    il.EmitCall(OpCodes.Callvirt, methInf, null);
    il.Emit(OpCodes.Pop);
    il.Emit(OpCodes.Ldloc, local.LocalIndex);
    il.Emit(OpCodes.Ret);


    Invoking this is 10 times faster than doing methodInfo.Invoke(...) (on a methodInfo object that was created only once, of course).






    share|improve this answer




























      0














      Thanks @MarcGravell (also for your deleted answer, which was of great help, as it spells out what you hint at in your comment to my question) and ILSpy, which helped me by compiling c# code to CIL, so I could just peek at that.



      So here's the final, working code:



      LocalBuilder local = il.DeclareLocal(typeof(ArraySegment<byte>));
      il.Emit(OpCodes.Ldarg_0);
      il.Emit(OpCodes.Ldloca, local);
      il.EmitCall(OpCodes.Callvirt, methInf, null);
      il.Emit(OpCodes.Pop);
      il.Emit(OpCodes.Ldloc, local.LocalIndex);
      il.Emit(OpCodes.Ret);


      Invoking this is 10 times faster than doing methodInfo.Invoke(...) (on a methodInfo object that was created only once, of course).






      share|improve this answer


























        0












        0








        0






        Thanks @MarcGravell (also for your deleted answer, which was of great help, as it spells out what you hint at in your comment to my question) and ILSpy, which helped me by compiling c# code to CIL, so I could just peek at that.



        So here's the final, working code:



        LocalBuilder local = il.DeclareLocal(typeof(ArraySegment<byte>));
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldloca, local);
        il.EmitCall(OpCodes.Callvirt, methInf, null);
        il.Emit(OpCodes.Pop);
        il.Emit(OpCodes.Ldloc, local.LocalIndex);
        il.Emit(OpCodes.Ret);


        Invoking this is 10 times faster than doing methodInfo.Invoke(...) (on a methodInfo object that was created only once, of course).






        share|improve this answer














        Thanks @MarcGravell (also for your deleted answer, which was of great help, as it spells out what you hint at in your comment to my question) and ILSpy, which helped me by compiling c# code to CIL, so I could just peek at that.



        So here's the final, working code:



        LocalBuilder local = il.DeclareLocal(typeof(ArraySegment<byte>));
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldloca, local);
        il.EmitCall(OpCodes.Callvirt, methInf, null);
        il.Emit(OpCodes.Pop);
        il.Emit(OpCodes.Ldloc, local.LocalIndex);
        il.Emit(OpCodes.Ret);


        Invoking this is 10 times faster than doing methodInfo.Invoke(...) (on a methodInfo object that was created only once, of course).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 1:06

























        answered Nov 13 '18 at 0:56









        Eugene Beresovsky

        10.8k660110




        10.8k660110






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271752%2fil-code-to-call-method-with-out-parameter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python