【问题标题】:Using C# DLL in Python3 with Pythonnet在 Python3 中使用 C# DLL 和 Pythonnet
【发布时间】:2021-06-14 19:55:53
【问题描述】:

我在Python3中使用c# dll,我使用的模块是 DaveSkender/Stock.Indicators 它是用 C# 编写的,符合公共语言规范 (CLS)。 所以我把它编译为dll 并在 Python3 中导入,如下所示:

import clr
clr.AddReference(r'dll/Skender.Stock.Indicators')

到目前为止,它可以正常工作并且可以确保模块成功导入。 但问题是传递参数。 C# DLL 中有这样的方法:

public static IEnumerable<SmaResult> GetSma<TQuote>(
            IEnumerable<TQuote> history,
            int lookbackPeriod)
            where TQuote : IQuote
        {
            // WHATEVER
        }

所以我在 Python 中这样传递参数:

from System.Collections.Generic import List

// Definitely, Qoute is inherited from IQoute 
history_list = List[Qoute]()
Indicator.GetSma(history_list, int(1))

但是一直显示TypeError:

TypeError: No method matches given arguments for GetSma: (<class 'System.Collections.Generic.0, Culture=neutral, PublicKeyToken=null]]'>, <class 'int'>)

我不知道 Python 中哪种类型与 C# 兼容。 我应该在这个方法中传递什么? 我的意思是我应该在 Python 中为 IEnumerable 传递什么?

【问题讨论】:

  • 也许是其他参数? python中的int是怎么定义的?
  • 顺便说一下,您正在显示 GetSmaExtended 方法,但您正在使用(或尝试使用)GetSma 方法。
  • @Ralf 哦,感谢您对此感兴趣。我修复了它,但没有什么不同。 :)

标签: python c# clr python.net


【解决方案1】:

感谢@Ralf,我再次查看了有关GetSma() 的C# 代码。而且它是Generic方法,应该以Generic形式调用。所以我试着像下面这样调用这个方法:

Indicator.GetSma[Qoute](history_list, int(1))

有效!!如果有像我这样的人,希望这个答案会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    相关资源
    最近更新 更多