如何蟒蛇回调传递给C#函数调用 [英] How to pass python callback to c# function call
问题描述
我想用C#类从蟒蛇,使用单声道/ Ubuntu的python.net。
到目前为止,我设法做一个简单的函数调用一个参数工作。什么我现在要做的是通过一个python回调到C#函数调用。
我试过下面的下面的变化,但没有奏效。 ?有人可以显示如何使这项工作。
// C# - testlib.cs
类MC {
酒店的公共双方法1(INT N){
Console.WriteLine(执行方法1);
/ *。* /
}
公共双方法2(代表F){
Console.WriteLine(执行方法2);
/ * ...在某些时候... * /
做F()/ *也试过f.DynamicInvoke()* /
Console.WriteLine(完成执行方法2) ;
}
}
Python脚本
进口TESTLIB,系统
MC = testlib.MC()
mc.method1(10)#,工程
DEF F():
打印执行F
mc.method2(F)
#不知道与签名方法2的,不够公平...
#这是把它变成一个回调的正确方法?
F2 = System.AssemblyLoad(F)
#没有错误消息,但˚F似乎并没有被调用
mc.method2(F2)
试图通过动作
或函数功能
,而不仅仅是原始的功能:
我用的IronPython这里(因为现在我没有任何的安装单做我的机器,但根据Python.NET 文档我认为它应该工作
其实你的代码几乎确定,但你需要输入动作
或函数功能
委托取决于你所需要的东西。
Python代码:
进口CLR $ b $从b型导入* $ b $从b系统导入操作
clr.AddReferenceToFileAndPath(RYourPath\TestLib.dll)
进口TESTLIB
打印(你好)
MC = TestLib.MC()
打印(mc.method1(10))
DEF F(fakeparam):
打印EXEC F
mc.method2(操作[INT] (F))
这是一个控制台输出:
你好
执行方法1
42.0
执行方法2
EXEC˚F
已完成执行方法2
C#代码:
使用系统;
命名空间TESTLIB
{
公共类MC
{
公共双方法1(INT N)
{
Console.WriteLine(执行方法1);
回报率42.0;
/ *。* /
}
公共双方法2(代表F)
{
Console.WriteLine(执行方法2);
对象[] = paramToPass新的对象[1];
paramToPass [0] =新INT();
f.DynamicInvoke(paramToPass);
Console.WriteLine(完成执行方法2);
回报率24.0;
}
}
}
我读文档为Python.net 使用泛型一次也发现了这个的 Python.NET命名和泛型类型的分辨率看起来像你需要指定参数类型明确
A(反映)泛型类型定义(如果存在与$ b $泛型类型
定义b定基本名称,以及该名称的非通用型)。这泛型类型
定义可以绑定到使用[]语法封闭泛型类型。试图
实例使用高清()抛出一个TypeError泛型类型。
块引用>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 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 script
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)
解决方案Try to pass
Action
orFunc
instead of just raw function: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
orFunc
delegate depends on what you need.python code:
import clr from types import * from System import Action clr.AddReferenceToFileAndPath(r"YourPath\TestLib.dll") import TestLib print("Hello") mc = TestLib.MC() print(mc.method1(10)) def f(fakeparam): print "exec f" mc.method2(Action[int](f))
This is a console output:
Hello Executing method1 42.0 Executing method2 exec f Done executing method2
C# code:
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; } } }
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
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.
这篇关于如何蟒蛇回调传递给C#函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!