Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C# object from IronPython

I have the following C# code to compile it into 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 with IronPython, I get the following error.

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

What might be wrong?

ADDED

arith = Arith() should have been arith = MyMath.Arith()

like image 808
prosseek Avatar asked May 04 '26 11:05

prosseek


1 Answers

You should be doing the following:

from MyMath import Arith

Or:

from MyMath import *

Otherwise, you'll have to refer to the Arith class as MyMath.Arith.

like image 142
Rafe Kettler Avatar answered May 06 '26 23:05

Rafe Kettler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!