从IronPython调用C#对象 [英] Calling C# object from IronPython

查看:718
本文介绍了从IronPython调用C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的C#代码把它编译成汇编MyMath.dll

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;
        }
    }
}

和我有以下的IronPython代码中使用该对象。

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)

当我运行此代码的IronPython,我碰到下面的错误。

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

什么可能错了?

ARITH = ARITH()应该已经ARITH = MyMath.Arith()

arith = Arith() should have been arith = MyMath.Arith()

推荐答案

您应该做以下几点:

from MyMath import Arith

或者

from MyMath import *

否则, 将不得不参照 ARITH 类MyMath.Arith。

Otherwise, you'll have to refer to the Arith class as MyMath.Arith.

这篇关于从IronPython调用C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆