. NET Dynamic Scripting language

Source: Internet
Author: User
Tags silverlight

Script.net (s#) is a custom behavior that allows for your application, with local. NET objects, types, and components interact dynamically with the scripting language. The managed application itself can alter the default behavior of an XML configuration script runtime, replace the bound method, the property, and construct the native behavior. NET types, names, functions and types of parsing, and so on.

Script.net supports the following platforms:

    • . NET Framework 2.0, 3.5
    • Compact Framework 3.5
    • Silverlight 4
    • XBox (via XNA framework)

: http://www.protsyk.com/scriptdotnet/versions/latest/

After you create a new console project, add a ScriptDotNet.dll reference to the program code as follows

Using system;using scriptnet;using scriptnet.runtime;namespace testscript{    class program    {        static void Main (string[] args)        {            runtimehost.initialize ();            Run script.net script            s = script.compile (@ "                s= ' hello ' + ' + ' + ' world ';                Console.WriteLine (s); ");            S.execute ();            The code fragment that runs script.net            object obj = Script.runcode ("return;");            Console.WriteLine (obj);}}}    

Here are two classes:runtimehost,Script.


Runtimehost
is the core point of the integration of the script processing runtime infrastructure. It must be initialized before using the Script.net class library.

RuntimeHost.Initialize();

This is initialized by default configuration, but this default configuration for Win32 is different from Silverlight and CF.

The following overload can be used with custom configuration:

publicstatic voidInitialize(Stream configuration);

The parameter is a valid XML configuration file for the file stream.

When configuring settings, there are some customizable parts of the runtime:

    • Binder--The member used to bind the object;
    • Activator--Used to construct an instance of the object, and the default Activator binds the constructor of the type with binder;
    • Scope Factory--used to generate the scope of a given type;
    • Assembly and Type Manager--for loading assemblies, previewing their types, managing and resolving given names. The default assembly management for different platforms is different.
RuntimeHost.Binder = newDefaultBinder();RuntimeHost.Activator = new DefaultActivator();RuntimeHost.ScopeFactory = new ScopeFactory();RuntimeHost.AssemblyManager = newDefaultAssemblyManager();RuntimeHost.Initialize();

To clear all runtimehost information, you can call the

RuntimeHost.CleanUp();

  

Script for compiling code

(This figure is 1.0.0.3 and will be different from the current version)

In addition, from here script.net for. NET 3.5 (old version) can download an IDE for easy script debugging.

data types and Constants

  

Type name Corresponding. The type of net Initialization Constant
Double Double x=3.14; Digital string
Long Long X=1; Digital string
String String s= ' Hello world '; A string of symbols enclosed in single or double quotation marks.
bool bool Bool=true; True/false
Array Object[] a=[3.14, 1, "Hello World", true, S] Variable name or constant sequence enclosed in square brackets

Also null indicates that the object is empty

Operator

Operator category Operator
Basic

x.y

F (x)

A[i]

X + +

x--

One dollar

+

-

!

~

++

--

Multiplication

*

/

%

Addition

+

-

Relationship and type detection

<

>

>=

<=

Is

Equal

==

!=

Logical "and" &
Logical "or" |
Conditions and &&
Condition OR ||
Assign value

=

+=

-=

An expression

Examples are as follows:

X = (y+4) * *;
Y = a[5] + 8;
Z = MATH.SQRT (256);
P = new System.Drawing.Point (3,4);

Process Control

  Order (slightly)

Branch

if (expression) statement [ else if (expression) statement] else statement

       

if (1<0)        b=1;if (1<3)    b=2;else     b=3;   if (1<0)    b=1;else if (1>3)    b=2;else     b=3;     

  

Switch (expr)

{
Case Expr1:statement
...
default: statement
}

  

switch (i) {case    1:messagebox.show (' hello! ');    Case 2:messagebox.show ('? ');    Default:MessageBox.Show (' No '); }

Cycle

   for (Expression1; Expression2; Expression3) Statement

sum=0; for (i=0; i<10; i++)    sum = sum + a[i];
foreach(Identifier inchExpression) Statement
a=[1,2,3,4,5];
sum = 0; foreach (I in a)
sum = sum + i;
while(Expression) Statement
while (i<100)    i++;
Break, continue is used only in loop statements to jump out of a loop return for returning a function

Using statement block

using (Type or object) {
Statement
}
using (Math) {    return Pow (2,10);  } A = new list<|int|> (); using (a) {    Add];  } return a[0];

Try...catch....finally statement block

Try

{

....

}

catch (E)

{

......

}

Finally

{

......

}

Functions (function)

The definition syntax for a function is as follows

function functionname (p1,p2,... PN)

{

Statement

}

The following are the built-in functions of script.net

eval– the value of an expression and returns, example a = eval (' 2+3*4 ');

clear– empty context variable, obsolete

Array-Create arrays, example a = Array (' Alex ', ' Peter '); A is of type string[] and b = Array (' Alex ', 1, 2); B is of type object[]

Because script.net can call. NET's class library, its built-in functions are not much. When it comes to calling class libraries, it is unavoidable to load DLLs. There are two ways to load a DLL by creating a new Defaultassemblymanager object

Runtimehost.assemblymanager = new Defaultassemblymanager ();

, call its Addassembly method to add the assembly. (Note: Once you add an assembly in this way, you add all the DLLs you need to use, even the dll--mscorlib.dll that the console resides in). The other is added through its dedicated configuration file, which has the following configuration file format:

<?xml version= "1.0" encoding= "Utf-8"?><configuration>  <References>  </References>  <Types>  </Types>  <Scopes>  </Scopes>  <Operators>  </ operators>  <Settings>  </Settings>  <Initialization>    <![ cdata[    ]]>  </Initialization></Configuration>

Add a row within the References node, for example

  <References>    <assembly name= "System.Data, version=2.0.0.0, Culture=neutral, publickeytoken= b77a5c561934e089 "sn=" true "/>  </References>

All of the assemblies loaded into the Runtimehost will automatically have all the namespaces in the using assembly. This raises the security issue for the script, which remains to be resolved ...

. NET Dynamic Scripting language

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.