c# - 从 IronPython 调用 C# 对象

标签 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

可能出了什么问题?

已添加

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

最佳答案

您应该执行以下操作:

from MyMath import Arith

或者:

from MyMath import *

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

关于c# - 从 IronPython 调用 C# 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3902018/

相关文章:

c# - LINQ 中的条件和

c# - 使用非托管内存时出现奇怪的内存使用情况

python - 同时播放多个 wav 文件,用于 Python 中的鼓机

ironpython - 从 IronPython 初始化 C# List<T>?

python - IronPython:您曾使用 IronPython 而不是标准 .NET 语言(例如 C#)完成过哪些工作

c# - GridView 从抛出异常背后的代码更新

c# - 在没有内存泄漏的情况下替换大字符串中的文本

python - 无法在 Python 的 OpenCV 中加载 OpenCL 运行时

python - 基于行的图表绘制(Seaborn 或 Matplotlib)

javascript - Spotfire 中的图标变淡但没有消失