Script.net Implementing a Script library

Source: Internet
Author: User

The script.net itself does not implement the ability to invoke script libraries. If you put a bunch of already defined functions in a script file as a script library, you just need to make a little change to call the script library.

First, create an XML document that specifically stores which libraries the script will call, structured as follows

<?xml version= "1.0" encoding= "Utf-8"?><libmapping> <mainscript  scriptname= "" >    < Libscript libfilename= ""/>  </MainScript></LibMapping>

For example:

<?xml version= "1.0" encoding= "Utf-8"?><libmapping> <mainscript  scriptname= "Script1.txt" >    <libscript libfilename= "Lib1.txt"/>    <libscript libfilename= "Lib3.txt"/>  </mainscript >  <mainscript scriptname= "Script2.txt" >    <libscript libfilename= "Lib1.txt"/>    < Libscript libfilename= "Lib2.txt"/>  </MainScript></LibMapping>

Then parse the XML before executing the script, find out the script library it needs to call through the ScriptName property, read the script library file with the IO operation and add it to the script string, the code is as follows

            string script = String. Empty;            Script + = File.readalltext ("script1.txt") + "\ n";//Read the scripting file                        string libscript = string. Empty;            Parse XML file            XmlDocument xmldoc = new XmlDocument ();            Xmldoc.load ("Libmapping.xml");            XmlNode Mainnode = Xmldoc.selectsinglenode (string. Format ("//libmapping/mainscript[@scriptName = ' {0} ']", "script1.txt"));            if (Mainnode! = null)            {                XmlNodeList liblist = mainnode.childnodes;                Read Script library                foreach (XmlNode node in liblist)                {                    String fileName = node. attributes["Libfilename"]. Value;                    Libscript + = File.readalltext (fileName) + "\ n";                }            }            script+= Libscript;
Script s = script.compile (script);

The disadvantage of this is that debugging is inconvenient, if the script code error or there is an exception, simple string concatenation will make the error code of the number of lines may be incorrect, find the error code when there is difficulty, I hope that there is a better way to solve the hero.

Script.net Implementing a Script library

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.