简体   繁体   English

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

[英]how to use non static Dll functions written in c# , in Iron python?

I wrote this simple example in c# : 我在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;
             }

         }
    }
}

As you can see - in the first function i'm not using "static" . 正如你所看到的 - 在第一个函数中,我没有使用“静态”。 When I run in in Iron python using this code : 当我使用此代码在Iron python中运行时:

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

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

when I pass 2 args - I get this error : 当我通过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)

As I understand it asks for the fisrt argument to be from type "MyDllTest" but when trying to write : a = new MyDllTest I get an error. 据我所知,它要求fisrt参数来自“MyDllTest”类型,但在尝试写入时: a = new MyDllTest我收到错误。

what should I do? 我该怎么办? Any help would be very appreciated! 任何帮助将非常感谢!

sumFunc is an instance method, so you first need to create an instance of the class to be able to call the method. sumFunc是一个实例方法,因此您首先需要创建一个类的实例才能调用该方法。

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

from DLLTest import MyDllTest

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

C# non-static methods could be called only on the instance of the class and static methods can be called on the class itself. 只能在类的实例上调用C#非静态方法,并且可以在类本身上调用静态方法。

Static and instance methods 静态和实例方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM