Вызов C # объекта из IronPython - PullRequest
3 голосов
/ 11 октября 2010

У меня есть следующий код 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 Ответ

6 голосов
/ 11 октября 2010

Вы должны сделать следующее:

from MyMath import Arith

Или:

from MyMath import *

В противном случае вам придется ссылаться на класс Arith как MyMath.Arith.

...