在Python3和Pythonnet中使用C#DLL

2024-09-30 10:36:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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


Tags: 模块方法fromimport参数stockhistorypython3
1条回答
网友
1楼 · 发布于 2024-09-30 10:36:08

感谢@Ralf,我再次查看了C代码中关于GetSma()的内容。它是泛型方法,应该以泛型形式调用。因此,我尝试按如下方式调用此方法:

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

它起作用了!!希望这个答案能对像我这样的人有所帮助

相关问题 更多 >

    热门问题