Calling C# object from IronPython

别来无恙 提交于 2019-12-10 18:58:26

问题


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()


回答1:


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.



来源:https://stackoverflow.com/questions/3902018/calling-c-sharp-object-from-ironpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!