.NET 脚本(二)

类别:.NET开发 点击:0 评论:0 推荐:

版本更新历史:

更新 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 文件中的值将被脚本引擎调用。下面是这个段的一个例子。

<userPreferences defaultLanguage="C#" entryPoint="Main" 
  waitForUserAction="true" />

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

 <referencedAssemblies>
    <assembly path="System.dll" />
    <assembly path="System.Messaging.dll" />
    <assembly path="System.Messaging.dll" />
    <assembly path="System.Security.dll" />
</referencedAssemblies>

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

 
<supportedLanguages>
 
    <language name="JScript" 
assembly="C:\WINNT\Microsoft.NET\Framework\v1.1.4322\Microsoft.JScript.dll"
 
        codeProviderName="Microsoft.JScript.JScriptCodeProvider" />
 
    <language name="J#" 
assembly="c:\winnt\microsoft.net\framework\v1.1.4322\vjsharpcodeprovider.dll"
 
        codeProviderName="Microsoft.VJSharp.VJSharpCodeProvider" />
 
</supportedLanguages>      
      

更新 2/18/04: 版本 1.0.2.0

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

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

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

<language name="C#" /> Main() will be the 
  entry method to the assembly
<language name="C#" entryPoint"" /> Main() will 
  be the entry method to the assembly
<language name="C#" entryPoint"Stuff" /> Stuff() 
  will be the entry method to the assembly 

我还为.dnml XML 格式添加了一个可选的<waitForUserAction> 元素。这是一个新假如的特征,它可以让你当脚本执行完毕后仍保留控制台窗口。元素 waitForUserAction 是可选的。如果它没有包含在.dnml 文件中,那么窗口将会保留(打开)。该属性值可以是'true' 'false'。如果为真,窗口会保留。如果为假,当脚本执行完毕后控制台窗口会马上关闭。 这样可以让你把若干个脚本文件链接到一个批处理文件中来。

使用该元素的可能途径。

--nothing-- Console window will remain open after script has run
<waitForUserAction value="true"/> Console window will 
  remain open after script has run
<waitForUserAction value="True"/> Console window will 
  remain open after script has run
<waitForUserAction value="TRUE"/> Console window will 
  remain open after script has run
<waitForUserAction value="false"/> Console window will 
  close after script has run
<waitForUserAction value="False"/> Console window will 
  close after script has run
<waitForUserAction value="FALSE"/> Console window will 
  close after script has run

最后,我添加了.NET 脚本返回给调用进程, cmd或批处理文件,一个值的功能,该值可以是空的,也可以是一个整数。现在有两种定义脚本入口方法返回值的方式。你可以定义它为空,也可以定义它为一个整型值。如果你使用空值,脚本将不会返回任何东西。如果你使用整型值,脚本引擎将会返回给调用它的进程一个整型值。

两个不同的脚本入口方法的例子:

//The script engine will return nothing when this script is called.
public static void Main()
{
 //...do stuff
 return;
} 
//The script engine will return a 5 when this script is called.
public static int Main()
{
 //...do stuff
 return 5;
} 
//The script engine will return nothing when this script is called.
Public Shared Sub Main()
 '...do stuff
 return
End Sub
 
//The script engine will return a 5 when this script is called.
Public Shared Function Main() as Integer
 '...do stuff
 return 5
End Function

本文地址:http://com.8s8s.com/it/it44982.htm