How to export C# methods?

前端 未结 4 1926
醉酒成梦
醉酒成梦 2020-11-30 09:05

How can we export C# methods?

I have a dll and I want to use its methods in the Python language with the ctypes module. Because I need to use the ctypes module, I ne

4条回答
  •  失恋的感觉
    2020-11-30 09:40

    That's not possible. If you need DLL exports you'll need to use the C++/CLI language. For example:

    public ref class Class1 {
    public:
      static int add(int a, int b) {
          return a + b;
      }
    };
    
    extern "C" __declspec(dllexport) 
    int add(int a, int b) {
      return Class1::add(a, b);
    }
    

    The class can be written in C# as well. The C++/CLI compiler emits a special thunk for the export that ensures that the CLR is loaded and execution switches to managed mode. This is not exactly fast.

    Writing [ComVisible(true)] code in C# is another possibility.

提交回复
热议问题