【问题标题】:Calling C# object from IronPython从 IronPython 调用 C# 对象
【发布时间】:2010-10-11 04:43:00
【问题描述】:

我有以下 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 运行此代码时,我收到以下错误。

回溯(最近一次通话最后): 文件 ipycallcs,行未知,在 Initialize NameError:名称“Arith”未定义

可能出了什么问题?

添加

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

【问题讨论】:

  • 不应该是arith = MyMath.Arith()吗?
  • @Mark :是的,这就是问题所在,谢谢。
  • 要修复 IronPython 中的常见错误,试试这个Beginning IronPython

标签: c# python ironpython


【解决方案1】:

您应该执行以下操作:

from MyMath import Arith

或者:

from MyMath import *

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

【讨论】:

    猜你喜欢
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多