Script.net is a dynamic scripting language that enables programs to be extensible, customizable, and well maintained. Similar to the Office series VB Script, you can embed a large number of code blocks in your app to execute them at run time.
Script.net's design philosophy is: simple (simplicity), efficient (efficiency), easy to get started (intuitive). Let's start with an example to make a basic understanding.
Using System; using System.Diagnostics; using System.IO; using Scriptnet; using Scriptnet.runtime; void Main (string[] args) {runtimehost.initialize (); Script s = script.compile ("Console.WriteLine (' Hello World ');"); S.execute (); Runtimehost.cleanup (); } }}
To compile this example, first add a reference to the Assembly ScriptDotNet.dll, whose target is. NET 3.5. This example shows how script.net is invoking the type of the. NET framework and calling its methods. Let's look at one more example
Runtimehost.initialize (); Object Obj=script.runcode ("return;"); Console.WriteLine (obj);
This example demonstrates how to run the Script.net code snippet.
Data types, constants, expressions, statements
As a language, it usually contains syntax, data type, expression, statement, which is the basic content of the language level. Look at the table below,
Data types supported by Script.net
Constant, a Boolean constant is TRUE or false, NULL means that the object is empty, and the string is enclosed in single quotation marks.
Operational symbols: +,-, *,/,%,! , | , &,! =, >, <, is. Where are is used for object types.
Take a look at the following example expression,
X = (y+4) * *;
Y = a[5] + 8;
Z = MATH.SQRT (256);
P = new System.Drawing.Point (3,4);
The statement Script.net program is a collection of statements with three commonly used statements: order (sequencing), loop (loop), branch (branching)
If ... Then ... Else ... if (Expression) Statement Else statement
if (x>0) y = y + 1; else y = y? 1; if (x>0) message = ' x is positive ';
For ...
for(Expression1; Expression2; Expression3)
Statement
sum=0;
for (i=0; i<10; i++)
sum = sum + a[i];
Foreach ...
foreach(Identifier
inchExpression) Statement
arr=[1,2,3,4,5]; sum = 0;
foreach (i in arr) sum = sum + i;
While ...
while(Expression) Statement
while (i>0) i = i-1;
Switch
Switch (expr) {
Case Expr1:statement
...
default: statement
}
switch (i) {
Case 1:messagebox.show (' hello! ');
Case 2:messagebox.show ('? ');
Default:MessageBox.Show (' No ');
}
Break, Continue is used only in loop statements, to jump out of the loop return for return functions (function)
Functional function
Script.net is a scripting language, not an object-oriented OOP language. Typically, a code fragment is encapsulated into a function, and the definition syntax for the function is as follows
Function (ID1,ID2,..., IDN) {
Statement
function FAC (n) {
if (n==1) return 1;
else return N*FAC (n-1);
}
MessageBox.Show (FAC (5). ToString ());
Func_pointer = FAC;
Func_pointer (4);
下面的代码,演示了Script.NET与Host的交互,从Host中获取值
Runtimehost.initialize (); list<New list<int> (); Vals. AddRange (int[] {1, 2, 3, 4}); Script script = Script.compile (@ "rez = 0; foreach (number in numbers) rez + = number;"); Script. Context.setitem ("Numbers", vals);
运行程序,输出结果如下
Script Context Scripting contexts
The script context stores run-time information, variables, and introduced types, which can be introduced. NET object into the script to take advantage of the powerful features of the. NET Framework. The following example demonstrates the interoperability of script.net with host types
class program {static void Mai N (string[] args) {runtimehost.initialize (); Type type = Runtimehost.gettype ( "A"); object script = Script.runcode (@ "a=new A (); A.name= ' from Script.net '; return a. Name; "); Console.WriteLine (script); }} public class A {public string Name { Get Set } }
Define type A in host and reference it, which can be used directly in script.net. You can also define a type in host, and then upload its value to script.net, see example
Runtimehost.initialize (); Type type = Runtimehost.gettype ("from Host"; Script script = Script.compile (@ " AB. Name= ' from script.net '; Return AB. Name; "); Script. Context.setitem ("AB", a); script. Execute (); Console.WriteLine (script);
Start debugging, in script. The value of the Name property of object A is different before and after execute execution.
Runtime Configuration Run-time
At the root of the Script.net package, locate the configuration file Runtimeconfig.xml and add it to the project. It contains the initialization code that you can specify when you want to reference the assembly, type mappings, and run the Script.net engine.
<References>
<assembly name= "System.Data, version=2.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089" sn= "true"/>
</References>
<Types>
<type alias= "string" name= "System.String"/>
<type alias= "int" name= "System.Int32"/>
......
</Types>
<Initialization>
<! [cdata[
]]>
</Initialization>
For example, you can define numeric constants in the initialization area, or initialize the code as follows
<Initialization>
<! [cdata[
pi=3.14;
Console.WriteLine (' Jack is Right ');
]]>
</Initialization>
Then go back to the Script.net script code with the following code
Runtimehost.initialize (new FileStream ("Runtimeconfig.xml", FileMode.Open)); Script script = Script.compile (@ " Console.WriteLine (Pi); "); Script. Execute (); Console.WriteLine (script);
Running the program, you can see the results of the console email as follows
Note that the identifier of the script.net is case-sensitive, and if Pi is written as pi in the script call, the exception is reported.
Unhandled Exception:ScriptNET.Runtime.ScriptIdNotFoundException:Namespace PI is not found
Script.net the original name is s#, the document differs from the actual code. I was based on the right. NET understanding to infer its use, the cause of the error, plus it itself is an open source project, can be directly integrated into existing applications, error can also be the source code debugging. In this way, you can add code in your application code that will need to be dynamically compiled into a script.net script, adding flexibility to your application with the help of Script.net's powerful features.
. NET Dynamic scripting language script.net Getting Started Guide Quick start