简体   繁体   English

在 Python3 中使用 C# DLL 和 Pythonnet

[英]Using C# DLL in Python3 with Pythonnet

I'm using c# dll in Python3, and the module I'm using is DaveSkender/Stock.Indicators It is written in C# and complies with the Common Language Specification (CLS).我在 Python3 中使用 c# dll,我使用的模块是DaveSkender/Stock.Indicators它是用 C# 编写的,符合通用语言规范(CLS)。 So I compiled it as dll and imported in Python3 like below:所以我将它编译为 dll 并在 Python3 中导入,如下所示:

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

Until now, It works and can be sure that module imported successfully.到现在为止,它可以工作并且可以确保模块成功导入。 But the problem is passing parameter.但问题是传递参数。 There is a method like this in C# DLL: C# DLL中有这样的方法:

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

So I passed the params in Python like this:所以我像这样传递了 Python 中的参数:

from System.Collections.Generic import List

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

But it keeps showing TypeError:但它一直显示TypeError:

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

I don't know which type in Python is compatible with C#.我不知道Python中的哪个类型与C#兼容。 What should I pass in this method?我应该在这个方法中传递什么? I mean what should I pass for IEnumerable in Python?我的意思是我应该为 Python 中的 IEnumerable 传递什么?

Thank to @Ralf, I looked inside C# code again about GetSma() .感谢@Ralf,我再次查看了关于GetSma()的 C# 代码。 And it is Generic method, should be called in Generic form.而且它是Generic方法,应该以Generic形式调用。 So I tried to call this method like the below:所以我试着像下面这样调用这个方法:

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

It works.!有用。! Hope this answer to be helpful if there is someone like me.如果有像我这样的人,希望这个答案会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM