如何在Iron python中使用c#编写的非静态Dll函数?

时间:2013-08-25 11:47:48

标签: c# python dll instance ironpython

我在c#中写了这个简单的例子:

 using System;
 using System.Collections.Generic;
 using System.Linq;
  using System.Text;

  namespace DLLTest
  {
        public class MyDllTest
          {
                 public  int sumFunc(int a, int b)
                    {

        int sum = a + b;
        return sum;

    }

    public static string stringFunc(string a, int numT)
    {
           if (numT < 0)
            {
               string errStr = "Error! num < 0";
               return errStr;
            }
              else
             {
                 return a;
             }

         }
    }
}

正如你所看到的 - 在第一个函数中,我没有使用“静态”。 当我使用此代码在Iron python中运行时:

import sys
 import clr
clr.addReferenceToFileAndPath(...path do dll...)

from DLLTest import *
 res = MyDllTest.sumFunc(....HERE MY PROBLEM IS...)

当我通过2个args时 - 我收到此错误:

>>> res = MyDllTest.sumFunc(4,5)

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: sumFunc() takes exactly 3 arguments (2 given)

据我了解,它要求fisrt参数来自“MyDllTest”类型 但在尝试写作时:a = new MyDllTest我收到了错误。

我该怎么办? 任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:2)

sumFunc是一个实例方法,因此您首先需要创建一个类的实例才能调用该方法。

import clr
clr.addReferenceToFileAndPath(...path do dll...)

from DLLTest import MyDllTest

test = MyDllTest()
test.sumFunc(33, 44)

只能在类的实例上调用C#非静态方法,并且可以在类本身上调用静态方法。

Static and instance methods