Last active
March 26, 2021 22:21
-
-
Save AlexArcPy/a9dd03519c0e7ec2f58f1ede89d60803 to your computer and use it in GitHub Desktop.
Access C# code from Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import clr | |
import sys | |
print sys.version | |
sys.path.append(r"C:\GIS") | |
lb = clr.AddReference('SampleClassLibrary') #add .dll file | |
from SampleClassLibrary import Calculator | |
c = Calculator() | |
print "Result is", c.DoSum(1,2) | |
#2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] | |
#Result is 3 | |
#with nuget in VS - no need to install anything, works with standard Python installation | |
import ctypes | |
a = ctypes.cdll.LoadLibrary(r"c:\GIS\SampleClassLibrary.dll") | |
print a.add(3, 5) | |
#8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SampleClassLibrary | |
{ | |
public class Calculator | |
{ | |
public static int DoSum(int a, int b) | |
{ | |
return a + b; | |
} | |
} | |
} | |
//or with UnmanagedExports nuget in VS | |
using RGiesecke.DllExport; | |
using System.Runtime.InteropServices; | |
namespace SampleClassLibrary | |
{ | |
class Test | |
{ | |
[DllExport("add", CallingConvention = CallingConvention.Cdecl)] | |
public static int TestExport(int left, int right) | |
{ | |
return left + right; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment