📜  从 python 代码示例调用 c#

📅  最后修改于: 2022-03-11 14:46:39.156000             🧑  作者: Mango

代码示例2
"""
It is actually pretty easy. Just use NuGet to add the "UnmanagedExports" package 
to your .Net project. 
See https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports for details.
"""
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using RGiesecke.DllExport;

class Test
{
    [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
    public static int TestExport(int left, int right)
    {
        return left + right;
    }
}


"""
You can then load the dll and call the exposed methods in Python (works for 2.7)
"""
import ctypes
a = ctypes.cdll.LoadLibrary(source)
a.add(3, 5)