none
Invoking an internal static delegate via reflection RRS feed

  • Question

  • I need to vampire onto a static internal delegate on a class in an open-source library.  For commercial / legal reasons I don't want to just make it public and recompile, though I have tested that approach and it does work.

    To make it more entertaining, I actually need to call this delegate from IronPython.

    It's the ZeroMQ.ZPollItems.PollMany I need to call, in clrzmq4, https://github.com/swn1/clrzmq4/blob/ironpython/ZPollItems.cs

    The delegate is assigned a value during static class initialization by a horribly clever hack that injects platform-specific code; fortunately we don't need to know where it comes from to make the reflection work.

    When I search for "reflection" and "delegate" the results are completely swamped by articles about creating delegates for functions discovered using reflection, to speed up repeated invokations.  Nice information but not what I need.

    I need an example of invoking a delegate through reflection.


    Stephen W. Nuchia StatSoft, Inc. Tulsa, Oklahoma USA

    Wednesday, September 14, 2016 5:55 PM

Answers

  • I got it.  https://github.com/swn1/pyzmq/blob/ironpython/zmq/backend/cli/poll.py

    In IronPython, the static class has to be converted to a CLR type using clr.GetClrType.
    Then you get a field info structure with (type).GetField("name", BindingFlags.Static | BindingFlags.NonPublic).
    Then you get the delegate itself with fi.GetValue(None)

    The value is a callable object in IronPython, assign it to a global (or whatever) and call it like any other function. 


    Stephen W. Nuchia StatSoft, Inc. Tulsa, Oklahoma USA

    Wednesday, September 14, 2016 7:39 PM

All replies

  • I got it.  https://github.com/swn1/pyzmq/blob/ironpython/zmq/backend/cli/poll.py

    In IronPython, the static class has to be converted to a CLR type using clr.GetClrType.
    Then you get a field info structure with (type).GetField("name", BindingFlags.Static | BindingFlags.NonPublic).
    Then you get the delegate itself with fi.GetValue(None)

    The value is a callable object in IronPython, assign it to a global (or whatever) and call it like any other function. 


    Stephen W. Nuchia StatSoft, Inc. Tulsa, Oklahoma USA

    Wednesday, September 14, 2016 7:39 PM
  • Hi Stephen,

    I am glad to know that you solve the issue and thanks for share your solution to us.

    It will be beneficial to other community members which as similar issue.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 15, 2016 4:40 AM
    Moderator