Python instance--c# execute Python script, pass the parameter

Source: Internet
Author: User
Tags hosting python script

#-*-coding:utf-8-*-#The purpose of the first line is to let the code inside, can have Chinese annotation information. (or to run an error)#This Python script is used by C # to invoke.#simply test the effect of Hello world.defWelcome (name):return "Hello"+name#test the effect of a C # object for a parameter. (Get/Set properties of C # objects)deftestaddage (obj): obj. Age= obj. Age + 1obj. Desc= obj. UserName +"one year older in Python."#test the effect of a C # object for a parameter. (Methods for invoking C # objects)deftestAddAge2 (obj): obj. Addage (2)#test List.deftestlist (LST): Vresult=""     forEach_iteminchLst:vresult= Vresult +" "+Each_itemreturnVresult#test Set.defTestset (pSet): Vresult=""     forEach_iteminchPset:vresult= Vresult +" "+Each_itemreturnVresult#Test Dictionarydeftestdictionary (pdictionary): Vresult=""     forEach_iteminchPdictionary:vresult= Vresult +" "+ Each_item +"="+ Pdictionary[each_item] +";"    returnVresult

usingSystem;usingironpython.hosting;usingMicrosoft.Scripting.Hosting;namespaceconsoleapp1{/// <summary>    ///The test object. ///     ///used to pass data to a Python script/// </summary>     Public classTestdataobject {/// <summary>        ///user name. /// </summary>         Public stringUserName {Set;Get; } /// <summary>        ///age. /// </summary>         Public intAge {Set;Get; } /// <summary>        ///describe the information. /// </summary>         Public stringDesc {Set;Get; }  Public voidAddage (intAge ) {             This. Age = This. Age +Age ;  This. Desc = String.Format ("{0} is also large {1} years old in C #", This.        UserName, age); }         Public Override stringToString () {returnString.Format ("name: {0}; Age: {1}; Description: {2}", This. UserName, This. Age, This.        DESC); }    }     Public classRunpython { Public voidrunpythontest () {//loads an external Python script file.Scriptruntime Pyrumtime =Python.createruntime (); Dynamicobj = Pyrumtime.usefile ("testpythonfile.py"); // ==================================================            //simply call the method in the script file.Console.WriteLine (Obj.welcome ("Test C # call Python.")); Console.WriteLine (Obj.welcome ("test Chinese to see if it is normal! ")); // ==================================================            //test the custom object.Testdataobject testobj =NewTestdataobject () {UserName="Zhang San", Age= -, Desc="",            }; Console.WriteLine ("before invoking Script object data: {0}", Testobj);            Obj.testaddage (Testobj); Console.WriteLine ("Object Data ={0} after calling the Testaddage script", Testobj);            Obj.testaddage2 (Testobj); Console.WriteLine ("Object Data ={0} after calling the TestAddAge2 script", Testobj); // ==================================================            //test List.IronPython.Runtime.List testlist =NewIronPython.Runtime.List (); Testlist.add ("List Data 1"); Testlist.add ("List Data 2"); Testlist.add ("List Data 3"); //the test parameter is List.            stringresult =obj.testlist (testlist); Console.WriteLine ("call testlist, return result: {0}", result); // ==================================================            //test Set.IronPython.Runtime.SetCollection Testset =NewIronPython.Runtime.SetCollection (); Testset.add ("Set data 1"); Testset.add ("Set Data 2"); Testset.add ("Set Data 3"); //the test parameter is Set.result =Obj.testset (Testset); Console.WriteLine ("call Testset, return result: {0}", result); // ==================================================            //test Dictionary.IronPython.Runtime.PythonDictionary testdictionary =NewIronPython.Runtime.PythonDictionary (); testdictionary["Key1"] ="Value1"; testdictionary["Key2"] ="Value2"; testdictionary["Key3"] ="Value3"; //the test parameter is Dictionary.result =obj.testdictionary (testdictionary); Console.WriteLine ("call Testdictionary, return result: {0}", result);        Console.ReadLine (); }    }     }//--Run results//Hello Test C # call Python.//Hello test Chinese to see if it is normal! //before invoking Script object data: Name: Zhang San; age: 20; Description://after calling the Testaddage script, the object data = Name: Zhang San; age: 21; Description: Zhang San is one year older in Py//Thon.//after calling the TestAddAge2 script, the object data = Name: Zhang San; age: 23; Description: Zhang San is 2 years old in C #//call testlist, return result: List data 1 list data 2 list data 3//call Testset, return result: Set data 1 Set data 2 Set data 3//call Testdictionary, return result: key3=value3; Key2=value2; key1=value1;

Python instance--c# execute Python script, pass the parameter

Related Article

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.