小编Ghi*_*Dhu的帖子

在 Python 中为 .NET 实现 C# 接口

我得到了一个用 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我传递一个参数。

查看Python for .NET 源代码

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 …
Run Code Online (Sandbox Code Playgroud)

c# python interface python.net pythonnet

9
推荐指数
1
解决办法
3005
查看次数

标签 统计

c# ×1

interface ×1

python ×1

python.net ×1

pythonnet ×1