How to pass python callback to c# function call

前端 未结 2 661
滥情空心
滥情空心 2021-01-06 02:43

I am trying to use C# classes from python, using python.net on mono / ubuntu.

So far I managed to do a simple function call with one argument work. What I am now try

2条回答
  •  春和景丽
    2021-01-06 03:32

    It looks like you should define your Delegate explicitly:

    class MC {
        // Define a delegate type
        public delegate void Callback();
    
        public double method2(Callback f) {
            Console.WriteLine("Executing method2" );
            /* ... do f() at some point ... */
            /* also tried f.DynamicInvoke() */
            Console.WriteLine("Done executing method2" );
        }
    }
    

    Then from the Python code (this is a rough guess based from the docs):

    def f():
        print "Executing f"
    
    # instantiate a delegate
    f2 = testlib.MC.Callback(f)
    
    # use it
    mc.method2(f2)
    

提交回复
热议问题