漫画脚本举例,.NET 动态脚本语言Script.NET系列文章汇总 非常精彩的应用举例

对于Script.NET,我已经写了三篇文章来介绍它,文章汇总如下
.NET 动态脚本语言Script.NET 入门指南 Quick Start
.NET 动态脚本语言Script.NET 开发指南
.NET 动态脚本语言Script.NET 应用举例
希望这三篇文章能帮助你了解Script.NET。 下面的例子,继续讲解它的应用。
 

发送邮件 Send Email

mailObj = new MailMessage("[email protected]", "[email protected]","From Script.NET", "Body"); SMTPServer = new SmtpClient("smtp.163.com"); NTLMAuthentication = new System.Net.NetworkCredential("[email protected]", "password"); SMTPServer.UseDefaultCredentials = false; SMTPServer.Credentials = NTLMAuthentication; try { SMTPServer.Send(mailObj); } catch (ex) { Console.WriteLine(ex.Message); } finally { }
 

生成PDF文档 Generate PDF Document

对于PDF文件的操作,选择开源的iText library类库。脚本代码如下所示
//Create document at given location BeginDocument(“c:\\output.pdf”);//Change title in documentв’s meta dataВ ActiveDocument.AddTitle(‘Sample document’); //Create paragraphs with different alignment and color options Paragraph(‘Hello World’, ALIGN_CENTER); Paragraph(‘This is demo’, ALIGN_RIGHT, BLUE); Paragraph(‘This pdf was generated by S#’, GREEN); //Create a list of string items BeginList(); ListItem(‘One’); ListItem(‘Two’); ListItem(‘Three’); EndList(); //Create a table with tree columns BeginTable(3); //Create cells for the first row Cell(’1′); Cell(‘One’); Cell(Paragraph(‘Description of _disibledevent=>//Create cells for second row Cell(’2′); Cell(‘Two’); Cell(‘Description of Two’); EndTable(); //Flush and close document EndDocument();
生成PDF的应用,它的原文是《Using S# to generate PDF documents》,请找到这篇文章并下载代码体会。
通过这个例子,你也可以用它来生成Word/Excel文件。也许,一个动态生成文件的系统的方案产生于你的脑海中,根据用户选择的文件类型(PDF,DOC,XLS),动态调用这个脚本来生成相应类型的文件。这里的动态生成是有好处的,你可以不用编译程序,而只改变这里的脚本代码,来适应客户对文件内容格式(比如layout)的更改。
 

外壳命令 Shell Command

来实现一个拷贝文件的copy命令,代码如下
class Program { static void Main(string[] args) { RuntimeHost.Initialize(); Script script = Script.Compile(@" return Copy('a.xls','d:\Document'); "); script.Context.SetItem("Copy", new CopyFunction()); object result = script.Execute(); Console.WriteLine(result); Console.ReadLine(); } } public class CopyFunction : IInvokable { public bool CanInvoke() { return true; } public object Invoke(IScriptContext context, object[] args) { string sourceFile =Convert.ToString(args[0]); string destintionFolder=Convert.ToString(args[1]); if(!Directory.Exists(destintionFolder)) Directory.CreateDirectory(destintionFolder); string targetFile=Path.Combine(destintionFolder,Path.GetFileNameWithoutExtension( sourceFile)+Path.GetExtension( sourceFile)); File.Copy(sourceFile,targetFile); return targetFile; } }

有了这个做基础,你可以实现这样的功能:每日构建 Daily Build,请参考文章《图解持续集成--纯命令行实现.Net项目每日构建》
clip_image001.NET 动态脚本语言Script.NET系列文章汇总 非常精彩的应用举例漫画脚本举例
也可以做到这样的功能
clip_image001[6]clip_image001.NET 动态脚本语言Script.NET系列文章汇总 非常精彩的应用举例漫画脚本举例
原文作者使用的bat/cmd的Windows外壳命令,而这里使用的是Script.NET脚本,可以嵌入到其它应用程序中,被应用程序启动并执行。
 

访问SQL Server数据库

sql = DbProviderFactories.GetFactory("System.Data.SqlClient"); connection = sql.CreateConnection(); connection.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"; connection.Open(); command = sql.CreateCommand(); command.Connection = connection; command.CommandText = "select * from Customers"; reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["CompanyName"]+". "+ reader["ContactName"]); } connection.Dispose();
这个例子在前面已经举例过,它可以引深为对其他数据源的操作(MySQL,Oracel…)
 

工作流系统中的自定义代码活动

这个应用是我在思考工作流的规则编辑器时想到的,请看下图
imageclip_image001[6]clip_image001.NET 动态脚本语言Script.NET系列文章汇总 非常精彩的应用举例漫画脚本举例
我们知道,在自定义的工作流系统中,CodeActivity是最有价值的活动,可以做任何想做的事情,但也非常不好用。因为工作流的用户,不是做编程的,不懂C#.NET,所以,你不能指望他会改变这一点。Script.NET则弥补了这个缺陷,可以让工作流设计人员,在我的脚本编辑环境中,编辑脚本,给工作流添加灵活的脚本代码,在执行时,由Script.NET解析引擎执行。只要脚本编辑环境足够智能灵活,提供的Script Sample足够多,这个CodeActivity(应该改名叫ScriptActivity)为增加自定义的工作流系统的灵活性,发挥极大的作用。
 
 

自动化操作  Windows Automation

要达到这个功能,要参考Windows Automation API,请参考这里this article。
先来看一下应用的脚本是什么样子的,再来看实现原理
// Close existing instances of Notepad Kill(“notepad”); // Launch a new Notepad instance and get main window window = Launch(“notepad”); // Wait 1 second Wait(1000); // Get main editor region edit = FindByClassName(window, “Edit”); // focus main editor FocusEditor(edit); // Send sample text to the editor region SendKeys.SendWait(“Automating Notepad using Windows UI Automation and S#”); Wait(3000); // Find [File] menu mnuFile = FindById(window, “Item 1″); // Expand [File] menu Expand(mnuFile); Wait(1000); // Invoke [Save As] menu item InvokeById(window, “Item 4″); Wait(1000); // Get [Save As] dialog saveAsDialog = FindByName(window, “Save As”); // Get access to [FileName] textbox saveAsName = FindById(saveAsDialog, “1001″); // Focus filename editor FocusEditor(saveAsName); // Write down file name SendKeys.SendWait(“D:\\MyTextFile”); // Send [Enter] keypress SendKeys.SendWait(“{ENTER}”); Wait(1000); // Check whether Overwrite Dialog appeared confirmSaveAs = FindByName(saveAsDialog, “Confirm Save As”); if (confirmSaveAs != null) { // Click [OK] button InvokeById(confirmSaveAs, “CommandButton_6″); Wait(1000); } // Expand [File] menu Expand(mnuFile); Wait(1000); // Click [Exit] item InvokeById(window, “Item 7″);
这是在做什么,打开Notepad,在里面输入文字,最后保存文件,全部的实现都是用脚本来做的。这令我想到了自动化测试,UI自动化测试。确实是这样的,自动化的脚本代替了人工操作,完全不需要人为干预。
这个应用的原文是《Windows Automation: Automating Windows 7 Notepad within S# Script》。
 

动态窗体  Dynamic Silverlight Forms. Embedding S# Scripts into Xaml

对于Silverlight技术不熟悉,请用文章原文查看具体内容。我能理解到意思是,可以把以脚本的方式创建窗体,这样的窗体是动态的,而不编译时就写死的,自然是非常灵活的方法。
 
推荐一个小技巧,我想把Script.NET的脚本,像对待外壳命令一样,双击执行,或是右键点击执行,如下面的效果所示
imageimageclip_image001[6]clip_image001.NET 动态脚本语言Script.NET系列文章汇总 非常精彩的应用举例漫画脚本举例
先把Script.NET的原代码中的RunTests程序改造成可以接受一个文件参数的可执行文件,像这样
它的原来的用法是这样:Usage: RunTests.exe folderPath
改成这样的用法 Usage:  RunTests.exe  scriptFile
这样的用意是,让它接受一个Script.NET脚本文件名,并能执行它。
再到外壳中注册文件关联,比如我把Script.NET的文件扩展名定义为spt文件,并添加这样的注册表项
Windows Registry Editor Version 5.00
[-HKEY_CLASSES_ROOT\SystemFileAssociations\.spt\shell\RunTests]
这样就在外壳命令中关联了Script.NET的脚本文件spt和它的执行程序RunTests,达到可以像bat文件一样,双击执行。
 
总结:本质上,Script.NET的脚本运行还是解析为DotNet代码的执行,所以不必怀疑它能做什么,.NET能实现的功能,都能做到。问题是我们是否需要这样的动态脚本,来增强程序的可扩展性,灵活性,这取决于你,it is up to you。
Tags:  cs脚本 脚本错误 梦三国脚本 script脚本 漫画脚本举例

延伸阅读

最新评论

发表评论