Today summarizes the Python and C # Interactive module pythonnet, in fact, Microsoft also has the corresponding interpreter, is IronPython, but after all IronPython still have a lot of things not inherited from the python side, so sometimes use is not so cool. But fortunately, the powerful community always surprises us, and Pythonnet is one of the things that surprises me, and it's a smooth way to interact with C #. But the information on the Internet is still too little, but eventually it is toss out. Here's the code.
#Coding=utf-8#The CLR is the public runtime environment, which is the core of the interaction with C #ImportCLRImportSYS#This module is best imported when importing the CLR, so you can use the AddReference methodImportSystemsys.path.append (R'D:\dll')#load the C#dll file pathCLR. Findassembly ('ClassLibrary5.dll')#load the C#dll file fromClasslibrary5.aaaaaImport*#Import Namespacesa= CLR. AddReference ('ClassLibrary5')#instantiate a C # class library so that it can be called, such as the source location of this class libraryC1=bbbbb ()Printa.locationc1.a ()PrintDir (System)
Code comments are quite clear, there are a few points to note, when loading the C#dll file, you must use the
Clr. The findassembly method instead of the Clr.addreference method, and when importing CLR modules, it is best to import the system module, as explained in the reason note, and finally note that the C # namespace must be imported as a module. The above is the approximate usage of pythonnet. Other uses will need to be studied in the future use of the process!
Interactive module for Python and C # pythonnet