如何将python回调传递给c#函数调用 [英] How to pass python callback to c# function call

查看:27
本文介绍了如何将python回调传递给c#函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 中的 C# 类,在 mono/ubuntu 上使用 python.net.

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

到目前为止,我设法用一个参数完成了一个简单的函数调用.我现在要做的是将 python 回调传递给 C# 函数调用.

So far I managed to do a simple function call with one argument work. What I am now trying to do is pass a python callback to the C# function call.

我尝试了以下变体,但均无效.有人可以展示如何使其工作吗?

I tried the following variations below, none worked. Can someone show how to make that work?

// C# - testlib.cs
class MC {
    public double method1(int n) {
        Console.WriteLine("Executing method1" );
        /* .. */
    }

    public double method2(Delegate f) {
        Console.WriteLine("Executing method2" );
        /* ... do f() at some point ... */
        /* also tried f.DynamicInvoke() */
        Console.WriteLine("Done executing method2" );
    }
}

Python 脚本

import testlib, System
mc = testlib.MC()
mc.method1(10) # that works

def f():
    print "Executing f"

mc.method2(f)  
# does not know of method2 with that signature, fair enough...

# is this the right way to turn it into a callback?
f2 = System.AssemblyLoad(f) 
# no error message, but f does not seem to be invoked
mc.method2(f2)              

推荐答案

尝试传递 ActionFunc 而不仅仅是原始函数:

Try to pass Action or Func instead of just raw function:

我在这里使用了 IronPython(因为现在我的任何机器上都没有安装 Mono,但是根据 Python.NET 文档 我认为它应该有效实际上你的代码几乎没问题,但你需要根据你的需要导入 ActionFunc 委托.

I used IronPython here (because right now I don't have mono installed on any of my machines but according of Python.NET documentation I think it should work Actually your code is almost ok but you need to import Action or Func delegate depends on what you need.

python 代码:

import clr
from types import *
from System import Action
clr.AddReferenceToFileAndPath(r"YourPathTestLib.dll")
import TestLib
print("Hello")
mc = TestLib.MC()
print(mc.method1(10))

def f(fakeparam):
    print "exec f" 

mc.method2(Action[int](f))

这是一个控制台输出:

Hello
Executing method1
42.0
Executing method2
exec f
Done executing method2

C# 代码:

using System;


namespace TestLib
{
    public class MC
    {
        public double method1(int n)
        {
            Console.WriteLine("Executing method1");
            return 42.0;
            /* .. */
        }

        public double method2(Delegate f)
        {
            Console.WriteLine("Executing method2");
            object[] paramToPass = new object[1];
            paramToPass[0] = new int();
            f.DynamicInvoke(paramToPass);
            Console.WriteLine("Done executing method2");
            return 24.0;

        }
    }
}

我再次阅读了 Python.net 的文档使用泛型,也发现了这个 Python.NET 泛型类型的命名和解析 看起来你需要指定显式参数类型

I read docs for Python.net Using Generics again and also found this Python.NET Naming and resolution of generic types look like you need to specify parameter type explicitly

引自那里:

一个(反射的)泛型类型定义(如果存在泛型类型定义与给定的基本名称,并且没有具有该名称的非泛型类型).这种通用类型可以使用 [] 语法将定义绑定到封闭的泛型类型.尝试去使用 () 实例化泛型类型 def 会引发 TypeError.

a (reflected) generic type definition (if there exists a generic type definition with the given base name, and no non-generic type with that name). This generic type definition can be bound into a closed generic type using the [] syntax. Trying to instantiate a generic type def using () raises a TypeError.

这篇关于如何将python回调传递给c#函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆