How to execute ironpython code in Silverlight

Source: Internet
Author: User

Suppose I already have a. py file named script. py, which may contain the following code:

1 def factorial (n ):
2 "factorial (N)-> Returns factorial of N"
3 if n <= 1: return 1
4 return N * factorial (n-1)
6 def welcome (name ):
7 Return "hello" + name


Now I want to call the factorial () function in the script. py file in the Silverlight program. We can use the following code:

[Method 1]

1 using Silverlight = Microsoft. Scripting. Silverlight;

3var runtime = Silverlight. dynamicengine. createruntime ();
4var Python = runtime. getengine ("Python ");
5 dynamic script = runtime. usefile ("script. py ");
6int P = script. factorial (5 );
7tbkresult. Text = P. tostring ();


[Method 2]

1 using Silverlight = Microsoft. Scripting. Silverlight;
2 using Microsoft. Scripting. Hosting;
4 scriptruntime runtime = Silverlight. dynamicengine. createruntime ();
5 scriptengine Python = runtime. getengine ("Python ");
6 scriptscope script = runtime. usefile ("script. py ");
7 func <int, int> factorial = script. getvariable <func <int, int> ("factorial ");
8 tbkresult. Text = factorial (5). tostring ();


However, to make the program run properly, you must add the following reference:

Ironpython. dll

Ironpython. modules. dll

Microsoft. Dynamic. dll

Microsoft. Scripting. debugging. dll

Microsoft. Scripting. dll

Microsoft. Scripting. Silverlight. dll


Microsoft. CSHARP. dll (this reference must be added to successfully use the dyanmic keyword)


Of course, pay attention to the storage path of the script. py file.


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: 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.