0

I am using 2 .net class libraries in the Python using Python.Net

Wanted to pass class object as parameter in a method call

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);
        }
    }
}

Using Jupyter Notebook

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)

Getting error

TypeLoadException Traceback (most recent call last) /var/folders/n7/k0yrxn111wx0wccws6chllth0000gn/T/ipykernel_5171/710681155.py in ----> 1 p3=obj.AddPoints(p1,p2)

TypeLoadException: Could not resolve type with token 01000011 from typeref (expected class 'System.Convert' in assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&) at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in :0

  • 1
    The error seems to say what it stands for preety clearly. Python.Net has no function like "Convert.ToInt32" because it is missing a reference to Windows dll. In that case python has no clue about `System.Convert` class. – Bartosz Olchowik Apr 26 '22 at 07:19
  • You are right `System.Convert` is causing error. To resolve it added `clr.AddReference("System")` But this also did not help. – user18949149 Apr 26 '22 at 08:19
  • probably there is no .dll in specific path for sys. Check if in the folder `/Users/user/Projects/CalculatorCheck/Calculator/bin/Debug/net5.0/` the dll exist. – Bartosz Olchowik Apr 26 '22 at 12:49

1 Answers1

0

You are probably using Python.NET 2.5, which does not support .NET 5.0.

You need to install Python.NET 3.0 or later, and you need to configure it to use the correct .NET runtime (by default Mono is used, which also does not support .NET 5).

LOST
  • 2,956
  • 3
  • 25
  • 40