calling method via reflection that takes an Action. All of the method, the containing class and T are marked...












0














I have an internal method like this



void Foo(Action<DB> act)


the class DB is in the same assembly and also marked internal. I can work out how to get a methodinfo for Foo but I cannot work out how to set up the callback act in the calling code. This is what I have so far (as an added spice I want to call another internal method from inside the callback)



    var tt = eng.GetType().Assembly;
var dd = tt.GetType("Internal.DB");
var kk = typeof(Action<>);
Type targs = { dd };
var qq = kk.MakeGenericType(targs);
MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq },null);
dynMethod.Invoke(this, new object {
((Action<object>)(db=>{
MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod2.Invoke(db);
})) });


my problem is here (I think)



((Action<object>)(db=>{


I really need



((Action<DB>)(db=>{


but I cannot do that (DB is internal), the one with object compiles but fails at run time saying that the object is of the wrong type.










share|improve this question


















  • 1




    Could you expand your example to be compileable?
    – thehennyy
    Nov 12 '18 at 19:32
















0














I have an internal method like this



void Foo(Action<DB> act)


the class DB is in the same assembly and also marked internal. I can work out how to get a methodinfo for Foo but I cannot work out how to set up the callback act in the calling code. This is what I have so far (as an added spice I want to call another internal method from inside the callback)



    var tt = eng.GetType().Assembly;
var dd = tt.GetType("Internal.DB");
var kk = typeof(Action<>);
Type targs = { dd };
var qq = kk.MakeGenericType(targs);
MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq },null);
dynMethod.Invoke(this, new object {
((Action<object>)(db=>{
MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod2.Invoke(db);
})) });


my problem is here (I think)



((Action<object>)(db=>{


I really need



((Action<DB>)(db=>{


but I cannot do that (DB is internal), the one with object compiles but fails at run time saying that the object is of the wrong type.










share|improve this question


















  • 1




    Could you expand your example to be compileable?
    – thehennyy
    Nov 12 '18 at 19:32














0












0








0







I have an internal method like this



void Foo(Action<DB> act)


the class DB is in the same assembly and also marked internal. I can work out how to get a methodinfo for Foo but I cannot work out how to set up the callback act in the calling code. This is what I have so far (as an added spice I want to call another internal method from inside the callback)



    var tt = eng.GetType().Assembly;
var dd = tt.GetType("Internal.DB");
var kk = typeof(Action<>);
Type targs = { dd };
var qq = kk.MakeGenericType(targs);
MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq },null);
dynMethod.Invoke(this, new object {
((Action<object>)(db=>{
MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod2.Invoke(db);
})) });


my problem is here (I think)



((Action<object>)(db=>{


I really need



((Action<DB>)(db=>{


but I cannot do that (DB is internal), the one with object compiles but fails at run time saying that the object is of the wrong type.










share|improve this question













I have an internal method like this



void Foo(Action<DB> act)


the class DB is in the same assembly and also marked internal. I can work out how to get a methodinfo for Foo but I cannot work out how to set up the callback act in the calling code. This is what I have so far (as an added spice I want to call another internal method from inside the callback)



    var tt = eng.GetType().Assembly;
var dd = tt.GetType("Internal.DB");
var kk = typeof(Action<>);
Type targs = { dd };
var qq = kk.MakeGenericType(targs);
MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq },null);
dynMethod.Invoke(this, new object {
((Action<object>)(db=>{
MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod2.Invoke(db);
})) });


my problem is here (I think)



((Action<object>)(db=>{


I really need



((Action<DB>)(db=>{


but I cannot do that (DB is internal), the one with object compiles but fails at run time saying that the object is of the wrong type.







c# reflection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 19:21









pm100

25k1557104




25k1557104








  • 1




    Could you expand your example to be compileable?
    – thehennyy
    Nov 12 '18 at 19:32














  • 1




    Could you expand your example to be compileable?
    – thehennyy
    Nov 12 '18 at 19:32








1




1




Could you expand your example to be compileable?
– thehennyy
Nov 12 '18 at 19:32




Could you expand your example to be compileable?
– thehennyy
Nov 12 '18 at 19:32












2 Answers
2






active

oldest

votes


















0














I've changed very little of your example and got code which compiles and runs to completion. Is there a chance your DB.Wiz() is throwing an exception? If not we may need more info about which runtime is giving you this issue or a more complete example. Below example properly outputs "Hello, World!"



.Net 4.6.1 Solution



ConsoleApp project, has reference to Class Library namespace Internal. I've changed your dynMethod2.Invoke() to pass an empty object as your example did not compile with one parameter to Invoke. Also removed redundant parens around your lambda expression.



private static void Main(string args)
{
Internal.Eng eng = new Internal.Eng();

var tt = eng.GetType().Assembly;
var dd = tt.GetType("Internal.DB");
var kk = typeof(Action<>);
Type targs = { dd };

var qq = kk.MakeGenericType(targs);

MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq }, null);

dynMethod.Invoke(eng, new object {
(Action<object>)(db=>{
MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod2.Invoke(db, new object{ });
})
});
}




Class library Internal namespace.



using System;

namespace Internal
{
public class Eng
{
internal void Foo(Action<DB> act) => act(new DB());
}

internal class DB
{
internal void Wiz()
{
Console.WriteLine("Hello, World!");
}
}
}





share|improve this answer





















  • when I do this the dynMethod.Invoke throws 'System,.Reflection.TargetException, Object does not match target type'.I assumed this was because Action<object> != Action<DB>
    – pm100
    Nov 12 '18 at 20:58










  • @pm100 What runtime are you on
    – Neil
    Nov 12 '18 at 20:59










  • dang - I am so dumb, I was passing the wrong ins6ance type to the invoke - you correcte4d it and I didnt see. TY
    – pm100
    Nov 12 '18 at 21:03



















0














A bit hard figuring out exactly what it is you're trying to do, but if Wiz is a method of DB that accepts no parameters then I believe you're after something like this:



dynMethod.Invoke(this, new object {
((Action<DB>)(db=> {
MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod2.Invoke(db, null);
})) });





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%2f53268758%2fcalling-method-via-reflection-that-takes-an-actiont-all-of-the-method-the-co%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I've changed very little of your example and got code which compiles and runs to completion. Is there a chance your DB.Wiz() is throwing an exception? If not we may need more info about which runtime is giving you this issue or a more complete example. Below example properly outputs "Hello, World!"



    .Net 4.6.1 Solution



    ConsoleApp project, has reference to Class Library namespace Internal. I've changed your dynMethod2.Invoke() to pass an empty object as your example did not compile with one parameter to Invoke. Also removed redundant parens around your lambda expression.



    private static void Main(string args)
    {
    Internal.Eng eng = new Internal.Eng();

    var tt = eng.GetType().Assembly;
    var dd = tt.GetType("Internal.DB");
    var kk = typeof(Action<>);
    Type targs = { dd };

    var qq = kk.MakeGenericType(targs);

    MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq }, null);

    dynMethod.Invoke(eng, new object {
    (Action<object>)(db=>{
    MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
    dynMethod2.Invoke(db, new object{ });
    })
    });
    }




    Class library Internal namespace.



    using System;

    namespace Internal
    {
    public class Eng
    {
    internal void Foo(Action<DB> act) => act(new DB());
    }

    internal class DB
    {
    internal void Wiz()
    {
    Console.WriteLine("Hello, World!");
    }
    }
    }





    share|improve this answer





















    • when I do this the dynMethod.Invoke throws 'System,.Reflection.TargetException, Object does not match target type'.I assumed this was because Action<object> != Action<DB>
      – pm100
      Nov 12 '18 at 20:58










    • @pm100 What runtime are you on
      – Neil
      Nov 12 '18 at 20:59










    • dang - I am so dumb, I was passing the wrong ins6ance type to the invoke - you correcte4d it and I didnt see. TY
      – pm100
      Nov 12 '18 at 21:03
















    0














    I've changed very little of your example and got code which compiles and runs to completion. Is there a chance your DB.Wiz() is throwing an exception? If not we may need more info about which runtime is giving you this issue or a more complete example. Below example properly outputs "Hello, World!"



    .Net 4.6.1 Solution



    ConsoleApp project, has reference to Class Library namespace Internal. I've changed your dynMethod2.Invoke() to pass an empty object as your example did not compile with one parameter to Invoke. Also removed redundant parens around your lambda expression.



    private static void Main(string args)
    {
    Internal.Eng eng = new Internal.Eng();

    var tt = eng.GetType().Assembly;
    var dd = tt.GetType("Internal.DB");
    var kk = typeof(Action<>);
    Type targs = { dd };

    var qq = kk.MakeGenericType(targs);

    MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq }, null);

    dynMethod.Invoke(eng, new object {
    (Action<object>)(db=>{
    MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
    dynMethod2.Invoke(db, new object{ });
    })
    });
    }




    Class library Internal namespace.



    using System;

    namespace Internal
    {
    public class Eng
    {
    internal void Foo(Action<DB> act) => act(new DB());
    }

    internal class DB
    {
    internal void Wiz()
    {
    Console.WriteLine("Hello, World!");
    }
    }
    }





    share|improve this answer





















    • when I do this the dynMethod.Invoke throws 'System,.Reflection.TargetException, Object does not match target type'.I assumed this was because Action<object> != Action<DB>
      – pm100
      Nov 12 '18 at 20:58










    • @pm100 What runtime are you on
      – Neil
      Nov 12 '18 at 20:59










    • dang - I am so dumb, I was passing the wrong ins6ance type to the invoke - you correcte4d it and I didnt see. TY
      – pm100
      Nov 12 '18 at 21:03














    0












    0








    0






    I've changed very little of your example and got code which compiles and runs to completion. Is there a chance your DB.Wiz() is throwing an exception? If not we may need more info about which runtime is giving you this issue or a more complete example. Below example properly outputs "Hello, World!"



    .Net 4.6.1 Solution



    ConsoleApp project, has reference to Class Library namespace Internal. I've changed your dynMethod2.Invoke() to pass an empty object as your example did not compile with one parameter to Invoke. Also removed redundant parens around your lambda expression.



    private static void Main(string args)
    {
    Internal.Eng eng = new Internal.Eng();

    var tt = eng.GetType().Assembly;
    var dd = tt.GetType("Internal.DB");
    var kk = typeof(Action<>);
    Type targs = { dd };

    var qq = kk.MakeGenericType(targs);

    MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq }, null);

    dynMethod.Invoke(eng, new object {
    (Action<object>)(db=>{
    MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
    dynMethod2.Invoke(db, new object{ });
    })
    });
    }




    Class library Internal namespace.



    using System;

    namespace Internal
    {
    public class Eng
    {
    internal void Foo(Action<DB> act) => act(new DB());
    }

    internal class DB
    {
    internal void Wiz()
    {
    Console.WriteLine("Hello, World!");
    }
    }
    }





    share|improve this answer












    I've changed very little of your example and got code which compiles and runs to completion. Is there a chance your DB.Wiz() is throwing an exception? If not we may need more info about which runtime is giving you this issue or a more complete example. Below example properly outputs "Hello, World!"



    .Net 4.6.1 Solution



    ConsoleApp project, has reference to Class Library namespace Internal. I've changed your dynMethod2.Invoke() to pass an empty object as your example did not compile with one parameter to Invoke. Also removed redundant parens around your lambda expression.



    private static void Main(string args)
    {
    Internal.Eng eng = new Internal.Eng();

    var tt = eng.GetType().Assembly;
    var dd = tt.GetType("Internal.DB");
    var kk = typeof(Action<>);
    Type targs = { dd };

    var qq = kk.MakeGenericType(targs);

    MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type { qq }, null);

    dynMethod.Invoke(eng, new object {
    (Action<object>)(db=>{
    MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
    dynMethod2.Invoke(db, new object{ });
    })
    });
    }




    Class library Internal namespace.



    using System;

    namespace Internal
    {
    public class Eng
    {
    internal void Foo(Action<DB> act) => act(new DB());
    }

    internal class DB
    {
    internal void Wiz()
    {
    Console.WriteLine("Hello, World!");
    }
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 19:44









    Neil

    8751712




    8751712












    • when I do this the dynMethod.Invoke throws 'System,.Reflection.TargetException, Object does not match target type'.I assumed this was because Action<object> != Action<DB>
      – pm100
      Nov 12 '18 at 20:58










    • @pm100 What runtime are you on
      – Neil
      Nov 12 '18 at 20:59










    • dang - I am so dumb, I was passing the wrong ins6ance type to the invoke - you correcte4d it and I didnt see. TY
      – pm100
      Nov 12 '18 at 21:03


















    • when I do this the dynMethod.Invoke throws 'System,.Reflection.TargetException, Object does not match target type'.I assumed this was because Action<object> != Action<DB>
      – pm100
      Nov 12 '18 at 20:58










    • @pm100 What runtime are you on
      – Neil
      Nov 12 '18 at 20:59










    • dang - I am so dumb, I was passing the wrong ins6ance type to the invoke - you correcte4d it and I didnt see. TY
      – pm100
      Nov 12 '18 at 21:03
















    when I do this the dynMethod.Invoke throws 'System,.Reflection.TargetException, Object does not match target type'.I assumed this was because Action<object> != Action<DB>
    – pm100
    Nov 12 '18 at 20:58




    when I do this the dynMethod.Invoke throws 'System,.Reflection.TargetException, Object does not match target type'.I assumed this was because Action<object> != Action<DB>
    – pm100
    Nov 12 '18 at 20:58












    @pm100 What runtime are you on
    – Neil
    Nov 12 '18 at 20:59




    @pm100 What runtime are you on
    – Neil
    Nov 12 '18 at 20:59












    dang - I am so dumb, I was passing the wrong ins6ance type to the invoke - you correcte4d it and I didnt see. TY
    – pm100
    Nov 12 '18 at 21:03




    dang - I am so dumb, I was passing the wrong ins6ance type to the invoke - you correcte4d it and I didnt see. TY
    – pm100
    Nov 12 '18 at 21:03













    0














    A bit hard figuring out exactly what it is you're trying to do, but if Wiz is a method of DB that accepts no parameters then I believe you're after something like this:



    dynMethod.Invoke(this, new object {
    ((Action<DB>)(db=> {
    MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
    dynMethod2.Invoke(db, null);
    })) });





    share|improve this answer


























      0














      A bit hard figuring out exactly what it is you're trying to do, but if Wiz is a method of DB that accepts no parameters then I believe you're after something like this:



      dynMethod.Invoke(this, new object {
      ((Action<DB>)(db=> {
      MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
      dynMethod2.Invoke(db, null);
      })) });





      share|improve this answer
























        0












        0








        0






        A bit hard figuring out exactly what it is you're trying to do, but if Wiz is a method of DB that accepts no parameters then I believe you're after something like this:



        dynMethod.Invoke(this, new object {
        ((Action<DB>)(db=> {
        MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
        dynMethod2.Invoke(db, null);
        })) });





        share|improve this answer












        A bit hard figuring out exactly what it is you're trying to do, but if Wiz is a method of DB that accepts no parameters then I believe you're after something like this:



        dynMethod.Invoke(this, new object {
        ((Action<DB>)(db=> {
        MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
        dynMethod2.Invoke(db, null);
        })) });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 19:54









        Mark Feldman

        8,25311638




        8,25311638






























            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%2f53268758%2fcalling-method-via-reflection-that-takes-an-actiont-all-of-the-method-the-co%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini