从IronPython调用C#对象

3
我有以下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
可能出了什么问题?
添加的内容:
arith = Arith()应该是arith = MyMath.Arith()。

1
应该是 arith = MyMath.Arith() 吧? - Mark
@Mark:是的,那就是问题所在,谢谢。 - prosseek
要修复IronPython中的常见错误,请尝试使用Beginning IronPython - fireydude
1个回答

6
您应该执行以下操作:
from MyMath import Arith

或者:

from MyMath import *

否则,您将不得不将Arith类称为MyMath.Arith。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接