从IronPython调用C#对象

pro*_*eek 3 c# python ironpython

我有以下C#代码将其编译为MyMath.dll程序集.

namespace MyMath {
    public class Arith {
        public Arith() {}
        public int Add(int x, int y) {
            return x + y;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下IronPython代码来使用此对象.

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

import MyMath
arith = Arith()
print arith.Add(10,20)
Run Code Online (Sandbox Code Playgroud)

当我使用IronPython运行此代码时,我收到以下错误.

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

可能有什么问题?

添加

arith = Arith()应该是arith = MyMath.Arith()

Raf*_*ler 6

你应该做以下事情:

from MyMath import Arith
Run Code Online (Sandbox Code Playgroud)

要么:

from MyMath import *
Run Code Online (Sandbox Code Playgroud)

否则,您必须将该Arith类称为MyMath.Arith.