.NET 脚本(二)

版本更新历史 :

更新 3/18/04: 版本 2.0.0.1

这个版本太大了,以至于我想把它提到一个主版本号的升级。下面是修复和提高的列表。

一个 app.config 文件被添加进来, 用来替代在 .dnml 文件中重复的选项 XML 元素,如: waitForUserAction, 入口方法,脚本语言和常见的引用程序集。我还为脚本引擎添加了添加新语言的功能,因此你可以用任何语言编写 .NET 脚本文件,只要定义了该语言的 'CodeProvider' 类(在后面会更详细地提到)。在 app.config 文件中定义的值就如机器配置一样。也就是是或,这些基本值可以被 dnml 文件中的值所覆盖。 Dnml 文件中的值具有最高优先级,它将被脚本引擎使用。但是如果你的 .dnml 脚本文件没有定义任何配置, app.config 文件中的值将被使用。这样可以让你在 .dnml 文件中只定义实际的代码。

** 用户偏好配置段 : ** 一个用户偏好段被添加到 app.config 文件中。这个段定义了三个配置。默认语言,脚本入口和等待用户动作标志。默认语言用来确定当 dnml 文件中没有定义语言元素时脚本语言的语言。入口是指当 dnml 文件没有定义入口时脚本引擎将会调用的方法。 waitForUserAction 标签是一个布尔值,用来决定当脚本执行完毕后控制台窗口是否保留,并等待一个 crlf 按键。如果这没有在 dnml 文件中定义, config 文件中的值将被脚本引擎调用。下面是这个段的一个例子。

1<userpreferences defaultlanguage="C#" entrypoint="Main" waitforuseraction="true"></userpreferences>

** 程序集引用配置段 : ** 这个段被用来定义脚本执行需要的程序集。任何在该段定义的程序集都会在每个脚本运行时编译进来。只需要程序集名称,而不是完全路径名。下面是这个段的一个例子。

 1<referencedassemblies>
 2    
 3    
 4        <assembly path="System.dll"></assembly>
 5    
 6    
 7        <assembly path="System.Messaging.dll"></assembly>
 8    
 9    
10        <assembly path="System.Messaging.dll"></assembly>
11    
12    
13        <assembly path="System.Security.dll"></assembly>
14</referencedassemblies>

** 语言支持配置段 : ** 这个段让你动态为脚本引擎添加新的支持语言,而不需重新便宜引擎代码。属性 name 就是在 dnml 文件中定义的名称,或是在用户偏好段中的 defaultLanguage 属性。属性 assembly 就是包含 codeprovder 该语言实现的程序集完整路径名和文件名。属性 codeProviderName 就是该语言的 code provider 类,包含所在名字空间。查看一下类 AssemblyGenerator 的 LateBindCodeProvider() 方法可以了解一下我是怎样为脚本引擎添加该项功能的。

 1<supportedlanguages>
 2    
 3    
 4     
 5    
 6    
 7        <language assembly="C:\WINNT\Microsoft.NET\Framework\v1.1.4322\Microsoft.JScript.dll" codeprovidername="Microsoft.JScript.JScriptCodeProvider" name="JScript"></language>
 8    
 9    
10     
11    
12    
13        <language assembly="c:\winnt\microsoft.net\framework\v1.1.4322\vjsharpcodeprovider.dll" codeprovidername="Microsoft.VJSharp.VJSharpCodeProvider" name="J#"></language>
14    
15    
16     
17    
18    
19    </supportedlanguages>

更新 2/18/04: 版本 1.0.2.0

我修补了 Charlie pointed out 提到的用 DotNetScriptEngine.exe 注册 .dnml 文件扩展名关联的一个错误。

还为 dnml 文件格式添加了一个可选的 entryPoint 属性。这样可以让用户指定一个程序集入口而不仅仅是 "Main" 方法。如果属性 entryPoint 被一个方法名称填充,则该方法将成为入口点。否则, "Main" 将成为默认的程序集入口点。

元素 language 可以用以下三种方式定义。

1<language name="C#"></language>

Main() will be the

  entry method to the assembly
1<language entrypoint""="" name="C#"></language>

Main() will

  be the entry method to the assembly
1<language entrypoint"stuff"="" name="C#"></language>

Stuff()

  will be the entry method to the assembly 

我还为 .dnml XML 格式添加了一个可选的

  1<waitforuseraction> 元素。这是一个新假如的特征,它可以让你当脚本执行完毕后仍保留控制台窗口。元素  waitForUserAction  是可选的。如果它没有包含在  .dnml  文件中,那么窗口将会保留(打开)。该属性值可以是  'true'  或  'false'  。如果为真,窗口会保留。如果为假,当脚本执行完毕后控制台窗口会马上关闭。  这样可以让你把若干个脚本文件链接到一个批处理文件中来。 
  2
  3###  使用该元素的可能途径。 
  4    
  5    
  6    --nothing-- Console window will remain open after script has run
  7    
  8    
  9    <waitforuseraction value="true"></waitforuseraction> Console window will 
 10    
 11    
 12      remain open after script has run
 13    
 14    
 15    <waitforuseraction value="True"></waitforuseraction> Console window will 
 16    
 17    
 18      remain open after script has run
 19    
 20    
 21    <waitforuseraction value="TRUE"></waitforuseraction> Console window will 
 22    
 23    
 24      remain open after script has run
 25    
 26    
 27    <waitforuseraction value="false"></waitforuseraction> Console window will 
 28    
 29    
 30      close after script has run
 31    
 32    
 33    <waitforuseraction value="False"></waitforuseraction> Console window will 
 34    
 35    
 36      close after script has run
 37    
 38    
 39    <waitforuseraction value="FALSE"></waitforuseraction> Console window will 
 40    
 41    
 42      close after script has run
 43
 44最后,我添加了  .NET  脚本返回给调用进程,  cmd  或批处理文件,一个值的功能,该值可以是空的,也可以是一个整数。现在有两种定义脚本入口方法返回值的方式。你可以定义它为空,也可以定义它为一个整型值。如果你使用空值,脚本将不会返回任何东西。如果你使用整型值,脚本引擎将会返回给调用它的进程一个整型值。 
 45
 46两个不同的脚本入口方法的例子  : 
 47    
 48    
 49    _//The script engine will return nothing when this script is called._
 50    
 51    
 52    public static void Main()
 53    
 54    
 55    {
 56    
 57    
 58     _//...do stuff_
 59    
 60    
 61     return;
 62    
 63    
 64    } 
 65    
 66    
 67    _//The script engine will return a 5 when this script is called._
 68    
 69    
 70    public static int Main()
 71    
 72    
 73    {
 74    
 75    
 76     _//...do stuff_
 77    
 78    
 79     return 5;
 80    
 81    
 82    } 
 83    
 84    
 85    _//The script engine will return nothing when this script is called._
 86    
 87    
 88    Public Shared Sub Main()
 89    
 90    
 91     '...do stuff
 92    
 93    
 94     return
 95    
 96    
 97    End Sub
 98    
 99    
100     
101    
102    
103    _//The script engine will return a 5 when this script is called._
104    
105    
106    Public Shared Function Main() as Integer
107    
108    
109     '...do stuff
110    
111    
112     return 5
113    
114    
115    End Function</waitforuseraction>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus