The application of the Python programming language can help programmers improve development efficiency, and it is widely used. Here, we can first familiarize ourselves with the features that this language brings to us by understanding how Python calls. net framework.
- Python for in code Operation Method
- Main functions of the Python Thread class
- Analysis on the practical application of Python two-dimensional array
- Correct understanding of how to use Python sys. arg
- Python inheritance embodies object-oriented features
1. Install python2.5 and the corresponding pywin32
2. Download ironPython source code is C.
I use ironpython1.1and download ironpython_00001.rar from codeplex. Decompress.
3. Use vistual studio to create a. NET class library project named IronPython.
Test the c # source files (do not assemblyInfo. cs) in the ironMath directory of ironPython In the src subdirectory under the ironPython directory to your project directory.
Add the source files just tested in the project.
4. Modify AssemblyInfo. cs
[Assembly: ComVisible (false)]
Change false to true.
5. Check COM Interop registration in project => property => Generate
6. in project => property => signature, check the Assembly signature. When selecting a strong-name key file, select new and enter a file name, such as IronPython. you can also manually generate the key file and select.
In the method that Python calls. net framework, manually generate the method: Use the sn-k IronPython. snk command in the command line interface of. Net Framework.
7. compile the project to generate IronPython. dll
8. Use regasm IronPython. dll to register COM components on the. Net Framework command line interface.
9. In the. Net Framework command line interface, use gacutil-I IronPython. dll to add the Library to the global cache.
10. Create a. net framework class library. The Code is as follows:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace test
- {
- public class testclass
- {
- public string func()
- {
- return "hello world";
- }
- }
- }
11. Create test. py to call the test. dll class library just written. Code:
- import win32com
- import win32com.client
- eng=win32com.client.Dispatch("IronPython.Hosting.PythonEngine")
- eng.execute("import clr")
- eng.execute("import sys")
- eng.execute("""sys.path.append('c:\\\\test')""")
- eng.execute("print sys.path")
- eng.execute("""clr.AddReferenceToFile("abc.dll")""")
- eng.execute("from test import *")
- eng.execute("obj = testclass()")
- eng.execute("print obj.func()")
Note: eng.exe cute ("" sys. path. append ('C: \\\ test ')""")
Add the path of test. dll to sys. path of ironPython. If this parameter is not added, AddReferenceToFile will fail.
The above are all the steps for Python to call. net framework.