How to export C# methods?

一个人想着一个人 提交于 2019-11-27 04:38:49

Contrary to popular belief, this is possible.
See here.

With the normal Python implementation ("CPython"), you can't, at least not directly.

You could write native C wrappers around our C# methods using C++/CLI, and call these wrappers from Python.

Or, you could try IronPython. This lets you run Python code and call code in any .Net language, including C#.

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.

(This may no longer be relevant since SLaks has found that ingenious link, but I'll leave an edited version for reference...)

The "normal" way of exposing .NET/C# objects to unmanaged code (like Python) is to create a COM-callable wrapper for the C# DLL (.NET assembly), and call that using Python's COM/OLE support. To create the COM-callable wrapper, use the tlbexp and/or regasm command-line utilities.

Obviously, however, this does not provide the C/DLL-style API that SLaks' link does.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!