Load .NET Assembly Using Python

To load .NET assemblies via Python you should use Python.NET. I choose Python.NET over IronPython, because IronPython seems less compatible with standard Python libraries (amongst other things).

Once you've downloaded and installed Python.NET (http://pythonnet.sourceforge.net/), you can begin the integration.

Integration Code Sample:
[code]

import sys

# import Python.NET module(s)
import CLR
from CLR.System.Reflection import Assembly

# get the root path (for the dlls)
pythonLoc = sys.path[0]
pythonLoc = pythonLoc[0:pythonLoc.rindex("\\")+1]

for dllName in ["MyDLL.dll",
"Microsoft.Practices.EnterpriseLibrary.Common.dll",
"Microsoft.Practices.EnterpriseLibrary.Data.dll"]:
   Assembly.LoadFile(pythonLoc+dllName)
# end for

# create instance of .NET class
csharpClass = clr.MyDLL.MyCsharpClass()

# call member of .NET class
result = csharpClass.SomeMethod()

[/code]

As you can see the key here is to use absolute paths. The variable 'pythonLoc' can be the path to any directory that contains your target .NET assembles.
If only Python provided intellectual property protection we would be in business!

Comments

Popular posts from this blog

Python SUDS with Windows Authentication (SOAP)

Text Sherlock the OpenGrok alternative (Source Code Search Engine)

How to remove, update, or replace the WKWebView inputAccessoryView