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)
5
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;
2...
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 ();
8...
[Method 2]
1 using Silverlight = Microsoft. Scripting. Silverlight;
2 using Microsoft. Scripting. Hosting;
3...
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 ();
9...
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.