1. 程式人生 > >.NET 動態腳本語言

.NET 動態腳本語言

tab compile fin get 腳本語言 sin php 信息 ring

Script.NET (S#) 是一種允許為你的應用程序自定義行為,與本地.NET對象、類型和組件交互動態的腳本語言。托管應用程序本身可以改變一個xml配置腳本運行時的默認行為,更換綁定的方法,屬性,構造原生的行為.NET類型,名稱,函數和類型的解析等。

Script.NET 支持以下平臺:

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

下載地址:http://www.protsyk.com/scriptdotnet/versions/latest/

新建一個控制臺項目之後,添加ScriptDotNet.dll的引用,程序代碼如下

技術分享
using System;
using ScriptNET;
using ScriptNET.Runtime;

namespace TestScript
{
    class Program
    {
        static void Main(string[] args)
        {
            RuntimeHost.Initialize();

            //運行Script.NET腳本
            Script s = Script.Compile(@"
                s=‘hello‘+‘ ‘+‘world‘;
                Console.WriteLine(s);");
            s.Execute();

            //運行Script.NET的代碼片段
            object obj = Script.RunCode(" return 1+1;");
            Console.WriteLine(obj);
        }
    }
}
技術分享

這裏用到兩個類:RuntimeHostScript


RuntimeHost
是腳本處理運行時基礎設施的集成核心點。它必須在使用script.net 類庫前初始化。

RuntimeHost.Initialize();

  這是通過默認配置初始化運行時,但這個對於Win32的默認配置與Silverlight 和CF是不同的。

下面這個重載可以使用自定義配置:

public static void Initialize(Stream configuration);

  參數是有效的XML配置文件的文件流。

在配置設置時,運行時有一些可定制的部分:

  • Binder -- 用於綁定對象的成員;
  • Activator -- 用於構造對象的實例,默認的Activator 用binder來綁定類型的構造函數;
  • Scope Factory -- 用於生成給定類型的作用域;
  • Assembly and Type Manager -- 用於載入程序集,預覽它們的類型,管理及解決給定的名稱。不同平臺的默認程序集管理是不同的。
RuntimeHost.Binder = new DefaultBinder(); RuntimeHost.Activator = new DefaultActivator(); RuntimeHost.ScopeFactory = new ScopeFactory(); RuntimeHost.AssemblyManager = new DefaultAssemblyManager(); RuntimeHost.Initialize();

  若要清除RuntimeHost的所有信息,可以調用

RuntimeHost.CleanUp();

  

Script用於編譯代碼

技術分享

(這圖是1.0.0.3的,會與現今版本的有差異)

此外,從這裏Script.NET for .NET 3.5 (old version)可以下載一個IDE,方便腳本的調試。

技術分享

數據類型和常量

  

類型名稱 對應.NET的類型 初始化 常量
double double x=3.14; 數字串
long long x=1; 數字串
string string s=‘Hello World’; 單引號或雙引號括起來的符號串
bool bool bool=true; true/false
array object[] a=[3.14 , 1 , "Hello world" , true , s ] 由方括號括起的變量名或常量序列

此外null表示對象為空

運算符

運算符類別   運算符
基本

x.y

f(x)

a[i]

x++

x--

一元

+

-

!

~

++

--

乘法

*

/

%

加法

+

-

關系和類型檢測

<

>

>=

<=

is

相等

==

!=

邏輯“與” &
邏輯“或” |
條件 AND &&
條件 OR ||
賦值

=

+=

-=

表達式

例子如下:

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

流程控制

  順序 (略)

  分支

  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 way‘); 
}

循環

  for(Expression1;Expression2;Expression3) Statement

sum=0; 
for(i=0; i<10; i++) 
   sum = sum + a[i];
  foreach (Identifier in Expression) 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 只用於循環語句中,用於跳出循環
return 用於返回函數

Using語句塊

using(type or object){
Statement
}
技術分享
using (Math) {
    return Pow(2,10);
  }


a = new List<|int|>();
using(a){
    Add(10);
  }
return a[0];
技術分享

try...catch....finally 語句塊

try

{

....

}

catch(e)

{

......

}

finally

{

......

}

函數 (function)

函數的定義語法如下

function functionName(p1,p2,...pn)

{

statement

}

以下是Script.NET的內置函數

eval – 求表達式的值並返回,例子 a = eval(‘2+3*4‘);

clear – 清空上下文變量,已經過時

array - 創建數組,例子 a = array(‘alex‘, ‘peter‘); // a is of type string[] 又如 b = array(‘alex‘, 1, 2); // b is of type object[]

  由於Script.NET可以調用.NET的類庫,所以其內置函數就不多了。說到調用類庫,免不了要加載dll。加載dll的方式有兩種,一種是通過創建新的DefaultAssemblyManager對象

 RuntimeHost.AssemblyManager = new DefaultAssemblyManager();

,調用其AddAssembly方法把Assembly逐一添加進去。(註意:一旦應用這種方式添加程序集,則要把所有需要用到的dll都添加進去,甚至連Console所在的dll——mscorlib.dll)。另一種是通過其專用的配置文件添加,配置文件格式如下:

技術分享
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <References>
  </References>
  <Types>
  </Types>
  <Scopes>
  </Scopes>
  <Operators>
  </Operators>
  <Settings>
  </Settings>
  <Initialization>
    <![CDATA[
    ]]>
  </Initialization>
</Configuration>
技術分享

在References節點內添加則行,例如

  <References>
    <Assembly name="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" sn="true"/>
  </References>

凡是加載進去的程序集,RuntimeHost都會自動using程序集裏所有的命名空間。這樣會引發腳本的安全性問題,此問題有待解決。。。

.NET 動態腳本語言