Analysis of Main steps for Python to call. net framework

Source: Internet
Author: User

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:

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace test  
  6. {  
  7. public class testclass  
  8. {  
  9. public string func()  
  10. {  
  11. return "hello world";  
  12. }  
  13. }  

11. Create test. py to call the test. dll class library just written. Code:

 
 
  1. import win32com  
  2. import win32com.client  
  3. eng=win32com.client.Dispatch("IronPython.Hosting.PythonEngine")  
  4. eng.execute("import clr")  
  5. eng.execute("import sys")  
  6. eng.execute("""sys.path.append('c:\\\\test')""")  
  7. eng.execute("print sys.path")  
  8. eng.execute("""clr.AddReferenceToFile("abc.dll")""")  
  9. eng.execute("from test import *")  
  10. eng.execute("obj = testclass()")  
  11. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.