Python for. Net scripts call the DOTNET framework (ref and out parameter problems)

Source: Internet
Author: User

Pythonnet is similar to ironpython but is definitely different. Pythonnet allows you to write scripts to call. NET Framework or your own DLL. Is an open source project on SourceForge. The usage is to download a python interpreter. After installation, replace the original version with the corresponding version of the downloaded python. Net file. Http://pythonnet.sourceforge.net/, supporting to DOTNET 2.0.

For example, download Python 2.5 to C disk, path is c: \ python25, copy all the files in D: \ pythonnet-2.0-alpha2 \ python2.5-ucs2 directory to c: \ python25, now you can use it.

TryCode

  Import  System

Class Test ():
_ Id = 0
_ Name = " 123 "
Def _ Init __ (Self, ID, name ):
Self. _ id = ID
Self. _ name = Name

Def Main ():
App = Test ( 123 , " ASD " )
System. Console. writeline (App. _ id)
System. Console. writeline (App. _ name)
System. Console. readkey ()

If _ Name __ = ' _ Main __ ' :
Main ()

 

This is a consoleProgramOf course, you can also do winform. However, I think using Python directly does not seem to be of any benefit, but it is still very good to use it for calling.

But C # contains ref and out values. What should I do? All references are passed in Python, but it does not seem to work here.

 

  Import  System as s
Def Main ():
M = " 123 "
R = 0
X = S. int32.tryparse (M, R)
S. Console. writeline (r)
S. Console. readkey ()
If _ Name __ = ' _ Main __ ' :
Main ()
 
 
 
 
The output value of the above Code is 0, which is obviously incorrect. However, output X is garbled and its type is an array.
  Import  System as s
Def Main ():
M = " 123 "
R = 0
X = S. int32.tryparse (M, R)
S. Console. writeline (X [0])
S. Console. writeline (X [ 1 ])
S. Console. readkey ()
If _ Name __ = ' _ Main __ ' :
Main ()
 
 
The code can be modified. Output the first 1 of 1123, which should be a true value, and 123 is the converted value. The same applies to ref values.
  Import  Sharpbot. engin as engin
Import Hopeutil. html as HTML
Import System as s
Def Main ():
App = Engin. defaultcrawler ( " Http://www.163.com " );
Result = Html. htmlsimpledom. htmlsimplemodel ()
Suc = App. responsestring (result)
If Suc [0]:
S. Console. writeline (Suc [ 1 ]. Title)
S. Console. writeline (Suc [ 1 ]. Metacharset)
S. Console. writeline (Suc [ 1 ]. Body)
Else :
S. Console. writeline ( 123 )

If _ Name __ = ' _ Main __ ' :
Main ()
 
 
The above responsestring parameter in C # Is (ref htmlsimpledom. htmlsimplemodel result ). It is also returned to the array. It feels good to use the script to test the crawler engine. Using a script to operate DOTNET is different.

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.