”VBS腳本“可以用任何純

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供恒山網(wǎng)站建設(shè)、恒山做網(wǎng)站、恒山網(wǎng)站設(shè)計(jì)、恒山網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、恒山企業(yè)網(wǎng)站模板建站服務(wù),十年恒山做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
文本編輯
工具編寫(xiě),包括系統(tǒng)自帶的”記事本“。VB.NET當(dāng)然也可以。
如果你是需要語(yǔ)法提示之類的,VB.NET可以提供絕大部分的語(yǔ)法提示。畢竟
VBS
是
VB
的子集。
u3d支持c#和js兩種腳本,而且c#是最普遍的,咱們看unity用的編譯器,是c#的跨平臺(tái)開(kāi)源編譯器mono,而且是unity更改過(guò)得mono,這個(gè)編譯器是基于c#.net2.0的語(yǔ)法的,但是由于unity的更改,使得它也支持了一些.net3.5的語(yǔ)法,比如匿名函數(shù)lambda表.
非也,乃微軟新一代面向?qū)ο罂梢暬幊陶Z(yǔ)言。另外,vb.net和vb6.0及以前版本都有很大差別。屬.net平臺(tái)。.net framework現(xiàn)在最新的版本是3.5,建議你學(xué)習(xí)2.0版本即可,有機(jī)會(huì)再看看3.5。
NET本身提供了強(qiáng)大的腳本引擎,可以直接使用.NETCLR的任何編程語(yǔ)言作為腳本語(yǔ)言,如VB.NET、C#、JScript,J#等等。使用腳本引擎,我們可以動(dòng)態(tài)生成任意表達(dá)式、或動(dòng)態(tài)導(dǎo)入任意腳本文件,并在任意時(shí)候執(zhí)行。經(jīng)實(shí)踐發(fā)現(xiàn),我們可以使用至少兩種不同的方式在.NET中使用腳本引擎:VsaEngine和CodeDom。其實(shí),CodeDom不能算是真正的腳本引擎,它實(shí)際上是編譯器。但是我們完全可以利用CodeDom來(lái)模擬腳本引擎。使用Emit方法也能達(dá)到動(dòng)態(tài)生成可執(zhí)行代碼的目的,而且Emit生成的代碼不需要編譯,因此速度更快。但是Emit插入的實(shí)際上是匯編代碼,不能算是腳本語(yǔ)言。本文介紹如何以CodeDom方式來(lái)動(dòng)態(tài)生成可執(zhí)行代碼。如何在.NET中實(shí)現(xiàn)腳本引擎(CodeDom篇)沐楓網(wǎng)志1.構(gòu)造一個(gè)編譯器設(shè)置編譯參數(shù)編譯參數(shù)需要在CompilerParameters設(shè)置:CompilerOptions用于設(shè)置編譯器命令行參數(shù)IncludeDebugInformation用于指示是否在內(nèi)存在生成AssemblyGenerateInMemory用于指示是否在內(nèi)存在生成AssemblyGenerateExecutable用于指示生成的Assembly類型是exe還是dllOutputAssembly用于指示生成的程序文件名(僅在GenerateInMemory為false的情況)ReferencedAssemblies用于添加引用Assembly例如:theParameters.ReferencedAssemblies.Add("System.dll");創(chuàng)建指定語(yǔ)言的編譯器編譯需要由指定語(yǔ)言的CodeDomProvider生成。這里列舉一些.NET的CodeDomProvider:vb.netMicrosoft.VisualBasic.VBCodeProviderC#Microsoft.CSharp.CSharpCodeProviderjscriptMicrosoft.JScript.JScriptCodeProviderJ#Microsoft.VJSharp.VJSharpCodeProvider以C#為例,要?jiǎng)?chuàng)建C#編譯器,代碼如下://.NET1.1/1.0ICodeCompilercompiler=newMicrosoft.CSharp.CSharpCodeProvider().CreateCompiler();//.NET2.0ICodeCompilercompiler=(ICodeCompiler)newMicrosoft.CSharp.CSharpCodeProvider();下面是完整的創(chuàng)建編譯器的例子://////創(chuàng)建相應(yīng)腳本語(yǔ)言的編譯器///privatevoidcreateCompiler(stringstrLanguage,booldebugMode,stringstrAssemblyFileName){this.theParameters=newCompilerParameters();this.theParameters.OutputAssembly=System.IO.Path.Combine(System.IO.Path.GetTempPath(),strAssemblyFileName+".dll");this.theParameters.GenerateExecutable=false;this.theParameters.GenerateInMemory=true;if(debugMode){this.theParameters.IncludeDebugInformation=true;this.theParameters.CompilerOptions+="/define:TRACE=1/define:DEBUG=1";}else{this.theParameters.IncludeDebugInformation=false;this.theParameters.CompilerOptions+="/define:TRACE=1";}AddReference("System.dll");AddReference("System.Data.dll");AddReference("System.Xml.dll");strLanguage=strLanguage.ToLower();CodeDomProvidertheProvider;if("visualbasic"==strLanguage||"vb"==strLanguage){theProvider=newMicrosoft.VisualBasic.VBCodeProvider();if(debugMode)theParameters.CompilerOptions+="/debug:full/optimize-/optionexplicit+/optionstrict+/optioncompare:text/imports:Microsoft.VisualBasic,System,System.Collections,System.Diagnostics";elsetheParameters.CompilerOptions+="/optimize/optionexplicit+/optionstrict+/optioncompare:text/imports:Microsoft.VisualBasic,System,System.Collections,System.Diagnostics";AddReference("Microsoft.VisualBasic.dll");}elseif("jscript"==strLanguage||"js"==strLanguage){theProvider=newMicrosoft.JScript.JScriptCodeProvider();AddReference("Microsoft.JScript.dll");}elseif("csharp"==strLanguage||"cs"==strLanguage||"c#"==strLanguage){theProvider=newMicrosoft.CSharp.CSharpCodeProvider();if(!debugMode)theParameters.CompilerOptions+="/optimize";}//elseif("jsharp"==strLanguage||"vj"==strLanguage||"j#"==strLanguage)//{//theProvider=newMicrosoft.VJSharp.VJSharpCodeProvider();//if(!debugMode)//theParameters.CompilerOptions+="/optimize";//}elsethrownewSystem.Exception("指定的腳本語(yǔ)言不被支持。");this.theCompiler=theProvider.CreateCompiler();}//////添加引用對(duì)象。//////引用的文件名publicvoidAddReference(string__strAssemblyName){theParameters.ReferencedAssemblies.Add(__strAssemblyName);}注:在.NETFramework2.0中,由于CreateCompiler方法被標(biāo)記作廢。為避免產(chǎn)生編譯警告,可直接返回CodeDomProvider作為編譯器:this.theCompiler=(ICodeCompiler)theProvider;2.編譯源代碼編譯源代碼相當(dāng)簡(jiǎn)單,只需一條語(yǔ)句就搞定了:CompilerResultscompilerResults=compiler.CompileAssemblyFromSource(this.theParameters,this.SourceText);執(zhí)行后,可以從compilerResults取得以下內(nèi)容:NativeCompilerReturnValue編譯結(jié)果,用于檢查是否成功Errors編譯時(shí)產(chǎn)生的錯(cuò)誤和警告信息CompiledAssembly如果編譯成功,則返回編譯生成的Assembly示例函數(shù)://////編譯腳本。編譯前將清空以前的編譯信息。///CompilerInfo將包含編譯時(shí)產(chǎn)生的錯(cuò)誤信息。//////成功時(shí)返回True。不成功為False。publicboolCompile(){this.theCompilerInfo="";this.isCompiled=false;this.theCompiledAssembly=null;this.theCompilerResults=this.theCompiler.CompileAssemblyFromSource(this.theParameters,this.SourceText);if(this.theCompilerResults.NativeCompilerReturnValue==0){this.isCompiled=true;this.theCompiledAssembly=this.theCompilerResults.CompiledAssembly;}System.Text.StringBuildercompilerInfo=newSystem.Text.StringBuilder();foreach(CompilerErrorerrinthis.theCompilerResults.Errors){compilerInfo.Append(err.ToString());compilerInfo.Append("/r/n");}theCompilerInfo=compilerInfo.ToString();returnisCompiled;}3.執(zhí)行代碼使用Reflection機(jī)制就可以很方便的執(zhí)行Assembly中的代碼。我們假設(shè)編譯時(shí)使用的腳本代碼this.SourceText內(nèi)容如下:namespacetest{publicclassscript{staticpublicvoidMain(){MessageBox.Show("Hello");}}}則相應(yīng)的執(zhí)行代碼為:scriptEngine.Invoke("test.script","Main",null);Invoke函數(shù)內(nèi)容://////執(zhí)行指定的腳本函數(shù)(Method)。///如果指定的類或模塊名,以及函數(shù)(Method)、或參數(shù)不正確,將會(huì)產(chǎn)生VsaException/VshException例外。//////類或模塊名///要執(zhí)行的函數(shù)(Method)名字///參數(shù)(數(shù)組)///返回執(zhí)行的結(jié)果publicobjectInvoke(string__strModule,string__strMethod,object[]__Arguments){if(!this.IsCompiled||this.theCompiledAssembly==null)thrownewSystem.Exception("腳本還沒(méi)有成功編譯");Type__ModuleType=this.theCompiledAssembly.GetType(__strModule);if(null==__ModuleType)thrownewSystem.Exception(string.Format("指定的類或模塊({0})未定義。",__strModule));MethodInfo__MethodInfo=__ModuleType.GetMethod(__strMethod);if(null==__MethodInfo)thrownewSystem.Exception(string.Format("指定的方法({0}::{1})未定義。",__strModule,__strMethod));try{return__MethodInfo.Invoke(null,__Arguments);}catch(TargetParameterCountException){thrownewSystem.Exception(string.Format("指定的方法({0}:{1})參數(shù)錯(cuò)誤。",__strModule,__strMethod));}catch(System.Exceptione){System.Diagnostics.Trace.WriteLine(string.Format("執(zhí)行({0}:{1})錯(cuò)誤:{2}",__strModule,__strMethod,e.ToString()));returnnull;}}總結(jié):CodeDom可以很方便的隨時(shí)編譯源代碼,并動(dòng)態(tài)執(zhí)行。雖然作為腳本引擎,它沒(méi)有VsaEngine正規(guī)和方便,但作為一般應(yīng)用,也夠用了。并且結(jié)合Reflection機(jī)制,它的功能比VsaEngine更強(qiáng)大:它可以編譯任何提供CompilerProvider的CLR語(yǔ)言(目前.NET自帶的語(yǔ)言中都有)。當(dāng)然,它也有一些缺點(diǎn):它生成的Assembly不能動(dòng)態(tài)卸載。這在一般情況下不成問(wèn)題,因?yàn)橐粋€(gè)源代碼只需編譯一次,并載入執(zhí)行,并不需要?jiǎng)討B(tài)卸載。假如你需要做腳本編輯器時(shí),就要考慮這個(gè)問(wèn)題,因?yàn)橛锌赡芤粋€(gè)腳本會(huì)因?yàn)樾扌薷母亩煌5闹匦戮幾g,從而造成不停的產(chǎn)生新的Assembly,最后將導(dǎo)致內(nèi)存被大量占用。要解決這個(gè)問(wèn)題,需要將編譯器加載到獨(dú)立的AppDomain中,通過(guò)卸載AppDomain達(dá)到卸載所需的Assembly的目的。
分享題目:vb.net腳本 vbnet web編程
網(wǎng)頁(yè)地址:http://chinadenli.net/article14/hppige.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈、軟件開(kāi)發(fā)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容