Ghi*_*Dhu 9 c# python interface python.net pythonnet
我得到了一个用 C# 编写的库,我正在尝试使用 Python for .NET 调用它。
我需要一个实例的主类有一个构造函数,如:
GDhuClient(IGDhuSettings)
Run Code Online (Sandbox Code Playgroud)
没有(公开的)实现IGDhuSettings接口的类。当我创建一个 Python 类来实现它时,例如,
class PyGDhuSettings(IGDhuSettings):
...
Run Code Online (Sandbox Code Playgroud)
TypeError: interface takes exactly one argument如果我没有__new__方法或者我以正常方式定义一个方法,我会得到:
def __new__(cls):
return super().__new__(cls)
Run Code Online (Sandbox Code Playgroud)
如果我尝试实例化接口,就好像它是一个类,我要么得到相同的错误(没有或 >1 个参数),要么<whatever> does not implement IGDhuSettings我传递一个参数。
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Python.Runtime
{
/// <summary>
/// Provides the implementation for reflected interface types. Managed
/// interfaces are represented in Python by actual Python type objects.
/// Each of those type objects is associated with an instance of this
/// class, which provides the implementation for the Python type.
/// </summary>
internal class InterfaceObject : ClassBase
{
internal ConstructorInfo ctor;
internal InterfaceObject(Type tp) : base(tp)
{
var coclass = (CoClassAttribute)Attribute.GetCustomAttribute(tp, cc_attr);
if (coclass != null)
{
ctor = coclass.CoClass.GetConstructor(Type.EmptyTypes);
}
}
private static Type cc_attr;
static InterfaceObject()
{
cc_attr = typeof(CoClassAttribute);
}
/// <summary>
/// Implements __new__ for reflected interface types.
/// </summary>
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
{
var self = (InterfaceObject)GetManagedObject(tp);
int nargs = Runtime.PyTuple_Size(args);
Type type = self.type;
object obj;
if (nargs == 1)
{
IntPtr inst = Runtime.PyTuple_GetItem(args, 0);
var co = GetManagedObject(inst) as CLRObject;
if (co == null || !type.IsInstanceOfType(co.inst))
{
Exceptions.SetError(Exceptions.TypeError, $"object does not implement {type.Name}");
return IntPtr.Zero;
}
obj = co.inst;
}
else if (nargs == 0 && self.ctor != null)
{
obj = self.ctor.Invoke(null);
if (obj == null || !type.IsInstanceOfType(obj))
{
Exceptions.SetError(Exceptions.TypeError, "CoClass default constructor failed");
return IntPtr.Zero;
}
}
else
{
Exceptions.SetError(Exceptions.TypeError, "interface takes exactly one argument");
return IntPtr.Zero;
}
return CLRObject.GetInstHandle(obj, self.pyHandle);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有看到在没有 CoClass(没有定义)或已经有一个实现它的类的情况下在 Python 中实现 C# 接口的方法。
我在这里遗漏了一些细微差别,还是这是 Python 对 .NET 的限制?
GitHub 讨论:https : //github.com/pythonnet/pythonnet/issues/674
小智 13
我发现我需要将命名空间字段添加到实现该接口的 Python 类中。据我了解,该字段代表实现该接口的 Python 类的 .NET 命名空间。
举个例子。我制作了一个具有以下界面的 C# 库:
public interface ITestInterface
{
string StringAdd(string text, int number);
}
Run Code Online (Sandbox Code Playgroud)
以及一个使用接口的简单静态函数:
public static string InvokeStringAdd(ITestInterface testInterface, string text)
{
return "*** " + testInterface.StringAdd(text, 7) + " ***";
}
Run Code Online (Sandbox Code Playgroud)
然后在 Python 端,我定义了以下实现接口的类:
class TestInterfaceSubClass(ITestInterface):
__namespace__ = "MyNameSpace"
def StringAdd(self,text,x):
return 'My text <{}> with number <{}>'.format(text, x)
Run Code Online (Sandbox Code Playgroud)
请注意,我添加了命名空间字段并为其指定了“MyNameSpace”值。我认为任何非冲突的 .NET 命名空间名称都可以。
在 Python 中测试实现
test_interface_subclass = TestInterfaceSubClass()
print(TestClass.InvokeStringAdd(test_interface_subclass,'My Text'))
Run Code Online (Sandbox Code Playgroud)
返回
*** My text <My Text> with number <7> ***
Run Code Online (Sandbox Code Playgroud)
最后请注意,python 类 TestInterfaceSubClass 被赋予命名空间“MyNameSpace”。这意味着重新评估 Jupyter 笔记本中的类定义代码将导致 .NET 端的命名空间冲突,因此您必须为其提供不同的命名空间值或重新启动内核。
| 归档时间: |
|
| 查看次数: |
3005 次 |
| 最近记录: |