简体   繁体   中英

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

I wrote this simple example in 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 :

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 :

>>> 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.

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.

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.

Static and instance methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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