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 1958248
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T08:17:09+00:00 2026-05-17T08:17:09+00:00

In IronPython I am trying to call a PythonFunction with different numbers of arguments

  • 0

In IronPython I am trying to call a PythonFunction with different numbers of arguments from C#. For instance;

I want to do:

def foo(a, b):
     print a, b

def bar(a, b, c = None):
     print a, b, c

p = App.DynamicEvent()
p.addHandler(foo)
p.addHandler(bar)
p.invoke("Not", "Working")

where addHandler takes a single argument and somehow stores it in a list of methods to be invoked and invoke has a signature like this:

public virtual void invoke(params object[] tArgs)

Because I want to avoid making it specific to the PythonEngine (and thus engine.Operations.Invoke()), I’ve tried several ways of storing and implementing these things as delegates but I think the crux of my problem is that I don’t know how to store some kind of MulticastDelegate base type that is compatible with a PythonFunction?

Perhaps I want to implement my own DynamicInvoke method? Any thoughts and experience would be greatly appreciated!

The reason for wanting to do this is that I want to transparently map calls made from a sealed Javascript engine into IronPython via C#. i.e. in the Javascript call: Client.doThing("something", 4, {"key:"value"})
and handle it in the python with:

def doThing(s, i, d):
    pass

using the following dynamic event binding:

doThingEvent = App.DynamicEvent()
doThingEvent.addHandler(doThing)
WebBrowser.handleMethod("doThing", doThingEvent);
  • 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-17T08:17:09+00:00Added an answer on May 17, 2026 at 8:17 am

    My first idea was as digEmAll suggested but I came up with a better solution that doesn’t require the casting to System.Action or any invoke-time type checking or branching. Just overload addHandler to take a PythonFunction as well as a Delegate, meaning you can use your original code:

    def foo(a, b):
         print a, b
    
    def bar(a, b, c = None):
         print a, b, c
    
    p = DynamicEvent(engine)
    p.addHandler(foo)
    p.addHandler(bar)
    
    p.invoke("a", "b")
    p.invoke("a", "b", "c")
    

    With this C# code:

    public class DynamicEvent
    {
        private Dictionary<int, Action<object[]>> delegates = new Dictionary<int, Action<object[]>>();
        public ScriptEngine Engine { get; set; }
    
    
        public DynamicEvent(ScriptEngine engine)
        {
            Engine = engine;
        }
    
        public void addHandler(PythonFunction pythonFunction)
        {
            int args = (int) pythonFunction.func_code.co_nlocals;
            delegates.Add(args, a => Engine.Operations.Invoke(pythonFunction, a));
        }
    
        public void addHandler(Delegate d)
        {
            int args = d.Method.GetParameters().Length;
            delegates.Add(args, a => d.DynamicInvoke(a));
        }
    
        public void invoke(params object[] args)
        {
            Action<object[]> action;
            if(!delegates.TryGetValue(args.Length, out action))
                throw new ArgumentException("There is no handler that takes " + args.Length + " arguments!");
    
            action(args);
        }
    }
    

    Note that you need to add the engine to the script’s scope so you can use it in the constructor.

    Hope that helped!


    Note: It is possible to get a Delegate and execute it from a PythonFunction like this:

    Delegate target = pythonFunction.__code__.Target;
    var result = target.DynamicInvoke(new object[] {pythonFunction, args});
    

    but it is more platform dependant than using Engine.Operations.Invoke() and the Delegate signature is different to a ‘regular’ Delegate.

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

Sidebar

Related Questions

Is it possible to call managed code, specifically IronRuby or IronPython from unamanaged code
Writing some test scripts in IronPython, I want to verify whether a window is
I understand that IronPython is an implementation of Python on the .NET platform just
When my IronPython program gets to the line import wx I get this message:
I'm hosting IronPython 2.0 in a C#/Winforms application. I would like Python to be
Has anyone built a website with IronPython and ASP.NET. What were your experiences and
Does anyone know how to install IronPython 2.0 with NGEN'ed binaries using the MSI
According to this: http://www.codeplex.com/IronPython/Wiki/View.aspx?title=IP20VsCPy25Perf&referringTitle=IronPython%20Performance IronPython (Python for .Net) is faster than regular Python (cPython)
If I type this line in an MS-DOS command prompt window: ipy -X:ColorfulConsole IronPython
Does beautiful soup work with iron python? If so with which version of iron

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.