我在Python中使用两个.net类库,使用Python.Net
希望在方法调用中将类对象作为参数传递。
PT.cs
using System;
namespace Point
{
public class PT
{
public int x { get; set; }
public int y { get; set; }
public PT()
{
x = 0;y = 0;
}
public PT(int X,int Y)
{
x = X;y = Y;
}
}
}Cal.cs
using System;
using Point;
namespace Calculator
{
public class Cal
{
public double X { get; set; }
public double Y { get; set; }
public Cal()
{
X = 0.0;
Y = 0.0;
}
public Cal(double x, double y)
{
X = x;
Y = y;
}
public static double Add(double x,double y)
{
return x + y;
}
public double Add()
{
return X + Y;
}
public PT AddPoints(PT p1, PT p2)
{
int x = Convert.ToInt32(Add(p1.x, p2.x));
int y = Convert.ToInt32(Add(p1.y, p2.y));
return new PT(x,y);
}
}
}使用木星笔记本
import sys
import clr
sys.path.append(r"/Users/user/Projects/CalculatorCheck/Calculator/bin/Debug/net5.0/")
clr.AddReference("Calculator")
clr.AddReference("Point")
from Calculator import Cal
from Point import PT
p1=PT(10,10)
p2=PT(20,20)
p3=obj.AddPoints(p1,p2)获取误差
/var/folders/n7/k0yrxn111wx0wccws6chllth0000gn/T/ipykernel_5171/710681155.py in -->1 p3=obj.AddPoints(p1,p2)
System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,对象(包装托管到本地)的TypeLoadException对象(System.Runtime,Version=5.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a中的预期类'System.Convert‘)在System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj、System.Reflection.BindingFlags invokeAttr、System.Reflection.Binder binder、System.Object[]参数、System.Globalization.CultureInfo区域性)的System.Reflection.RuntimeMethodInfo.Invoke(System.Object obj、System.Reflection.BindingFlags invokeAttr、System.Reflection.Binder binder、System.Object[]参数、System.Globalization.CultureInfo文化) 0x0006a处无法解析令牌01000011的类型
发布于 2022-04-26 16:39:05
您可能使用的是Python.NET 2.5,它不支持.NET 5.0。
您需要安装Python.NET 3.0或更高版本,并且需要将其配置为使用正确的.NET运行时(默认情况下使用Mono,这也不支持.NET 5)。
https://stackoverflow.com/questions/72009690
复制相似问题