从IronPython调用C#对象

时间:2010-10-10 20:19:06

标签: c# python ironpython

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

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

我有以下IronPython代码来使用这个对象。

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

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

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

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

可能出现什么问题?

ADDED

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

1 个答案:

答案 0 :(得分:6)

您应该执行以下操作:

from MyMath import Arith

或者:

from MyMath import *

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