如何使用Pythonnet将数组传递给.net中的函数?

如何使用Pythonnet将数组传递给.net中的函数?,python,c#,python-3.x,python.net,Python,C#,Python 3.x,Python.net,我正在使用一个名为 我正试图通过C#form调用它。为此,我使用了PythonnetPython.Runtime.Dll。我的Python版本是3.7.8 在python中,使用以下方法很容易获取函数: mins, maxs = trendln.calc_support_resistance(h) 其中h=hist[-1000:]。关闭 类似地,我想通过C#尝试一下,如下所示: using (Py.GIL()) { trendln = Py.Import("trendln&q

我正在使用一个名为

我正试图通过C#form调用它。为此,我使用了Pythonnet
Python.Runtime.Dll
。我的Python版本是3.7.8

在python中,使用以下方法很容易获取函数:

mins, maxs = trendln.calc_support_resistance(h)
其中
h=hist[-1000:]。关闭

类似地,我想通过C#尝试一下,如下所示:

using (Py.GIL())
{
    trendln = Py.Import("trendln");                
    double[] h = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
    int a, b = trendln.calc_support_resistance(h);
}
但我得到了以下错误:

Python.Runtime.PythonException
  HResult=0x80131500
  Message=ValueError : h is not list, numpy ndarray or pandas Series of numeric values or a 2-tuple thereof
  Source=Python.Runtime
  StackTrace:
['  File "C:\\Python37\\lib\\site-packages\\trendln\\__init__.py", line 486, in calc_support_resistance\n    else: raise ValueError(\'h is not list, numpy ndarray or pandas Series of numeric values or a 2-tuple thereof\')\n']   at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
   at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw)
   at Python.Runtime.PyObject.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at testapplication.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\test\Desktop\C#\testapplication\testapplication\Form1.cs:line 31
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at testapplication.Program.Main() in C:\Users\test\Desktop\C#\testapplication\testapplication\Program.cs:line 19

请让我知道我能做些什么来避免这个错误。如何使用type传递和接收正确的信息?

我想,
h
变量不能初始化为列表

试着像这样初始化它

using (Py.GIL())
{
    trendln = Py.Import("trendln");
    dynamic h = new float[] { 1F, 2F, 3F };
    int a, b = trendln.calc_support_resistance(h);
}
这对我很有用:

using (Py.GIL())
{
    dynamic h = np.arange(1000);
    PyTuple b = new PyTuple(trendln.calc_support_resistance(h));
    
    Console.WriteLine(b.ToString());
}