Calling a C # Object from IronPython

I have the following C # code to compile it into the MyMath.dll assembly.

namespace MyMath {
    public class Arith {
        public Arith() {}
        public int Add(int x, int y) {
            return x + y;
        }
    }
}

And I have the following IronPython code to use this object.

import clr
clr.AddReferenceToFile("MyMath.dll")

import MyMath
arith = Arith()
print arith.Add(10,20)

When I run this code using IronPython, I get the following error.

Traceback (most recent call last):
  File ipycallcs, line unknown, in Initialize
NameError: name 'Arith' is not defined

What could be wrong?

ADDED

arith = Arith () should be arith = MyMath.Arith ()

+3
source share
1 answer

You should do the following:

from MyMath import Arith

Or:

from MyMath import *

Otherwise, you will have to refer to the class Arithas MyMath.Arith.

+6
source

Source: https://habr.com/ru/post/1768884/


All Articles