资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.net脚本 vbnet web编程

VB.NET可以编写VBS脚本吗

”VBS脚本“可以用任何纯

创新互联建站专业为企业提供恒山网站建设、恒山做网站、恒山网站设计、恒山网站制作等企业网站建设、网页设计与制作、恒山企业网站模板建站服务,十年恒山做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

文本编辑

工具编写,包括系统自带的”记事本“。VB.NET当然也可以。

如果你是需要语法提示之类的,VB.NET可以提供绝大部分的语法提示。毕竟

VBS

VB

的子集。

unity3D可以用VB.NET来写游戏脚本吗?

u3d支持c#和js两种脚本,而且c#是最普遍的,咱们看unity用的编译器,是c#的跨平台开源编译器mono,而且是unity更改过得mono,这个编译器是基于c#.net2.0的语法的,但是由于unity的更改,使得它也支持了一些.net3.5的语法,比如匿名函数lambda表.

vb.net是脚本语言吗?

非也,乃微软新一代面向对象可视化编程语言。另外,vb.net和vb6.0及以前版本都有很大差别。属.net平台。.net framework现在最新的版本是3.5,建议你学习2.0版本即可,有机会再看看3.5。

如何在.NET中实现脚本引擎

NET本身提供了强大的脚本引擎,可以直接使用.NETCLR的任何编程语言作为脚本语言,如VB.NET、C#、JScript,J#等等。使用脚本引擎,我们可以动态生成任意表达式、或动态导入任意脚本文件,并在任意时候执行。经实践发现,我们可以使用至少两种不同的方式在.NET中使用脚本引擎:VsaEngine和CodeDom。其实,CodeDom不能算是真正的脚本引擎,它实际上是编译器。但是我们完全可以利用CodeDom来模拟脚本引擎。使用Emit方法也能达到动态生成可执行代码的目的,而且Emit生成的代码不需要编译,因此速度更快。但是Emit插入的实际上是汇编代码,不能算是脚本语言。本文介绍如何以CodeDom方式来动态生成可执行代码。如何在.NET中实现脚本引擎(CodeDom篇)沐枫网志1.构造一个编译器设置编译参数编译参数需要在CompilerParameters设置:CompilerOptions用于设置编译器命令行参数IncludeDebugInformation用于指示是否在内存在生成AssemblyGenerateInMemory用于指示是否在内存在生成AssemblyGenerateExecutable用于指示生成的Assembly类型是exe还是dllOutputAssembly用于指示生成的程序文件名(仅在GenerateInMemory为false的情况)ReferencedAssemblies用于添加引用Assembly例如:theParameters.ReferencedAssemblies.Add("System.dll");创建指定语言的编译器编译需要由指定语言的CodeDomProvider生成。这里列举一些.NET的CodeDomProvider:vb.netMicrosoft.VisualBasic.VBCodeProviderC#Microsoft.CSharp.CSharpCodeProviderjscriptMicrosoft.JScript.JScriptCodeProviderJ#Microsoft.VJSharp.VJSharpCodeProvider以C#为例,要创建C#编译器,代码如下://.NET1.1/1.0ICodeCompilercompiler=newMicrosoft.CSharp.CSharpCodeProvider().CreateCompiler();//.NET2.0ICodeCompilercompiler=(ICodeCompiler)newMicrosoft.CSharp.CSharpCodeProvider();下面是完整的创建编译器的例子://////创建相应脚本语言的编译器///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("指定的脚本语言不被支持。");this.theCompiler=theProvider.CreateCompiler();}//////添加引用对象。//////引用的文件名publicvoidAddReference(string__strAssemblyName){theParameters.ReferencedAssemblies.Add(__strAssemblyName);}注:在.NETFramework2.0中,由于CreateCompiler方法被标记作废。为避免产生编译警告,可直接返回CodeDomProvider作为编译器:this.theCompiler=(ICodeCompiler)theProvider;2.编译源代码编译源代码相当简单,只需一条语句就搞定了:CompilerResultscompilerResults=compiler.CompileAssemblyFromSource(this.theParameters,this.SourceText);执行后,可以从compilerResults取得以下内容:NativeCompilerReturnValue编译结果,用于检查是否成功Errors编译时产生的错误和警告信息CompiledAssembly如果编译成功,则返回编译生成的Assembly示例函数://////编译脚本。编译前将清空以前的编译信息。///CompilerInfo将包含编译时产生的错误信息。//////成功时返回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.执行代码使用Reflection机制就可以很方便的执行Assembly中的代码。我们假设编译时使用的脚本代码this.SourceText内容如下:namespacetest{publicclassscript{staticpublicvoidMain(){MessageBox.Show("Hello");}}}则相应的执行代码为:scriptEngine.Invoke("test.script","Main",null);Invoke函数内容://////执行指定的脚本函数(Method)。///如果指定的类或模块名,以及函数(Method)、或参数不正确,将会产生VsaException/VshException例外。//////类或模块名///要执行的函数(Method)名字///参数(数组)///返回执行的结果publicobjectInvoke(string__strModule,string__strMethod,object[]__Arguments){if(!this.IsCompiled||this.theCompiledAssembly==null)thrownewSystem.Exception("脚本还没有成功编译");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})参数错误。",__strModule,__strMethod));}catch(System.Exceptione){System.Diagnostics.Trace.WriteLine(string.Format("执行({0}:{1})错误:{2}",__strModule,__strMethod,e.ToString()));returnnull;}}总结:CodeDom可以很方便的随时编译源代码,并动态执行。虽然作为脚本引擎,它没有VsaEngine正规和方便,但作为一般应用,也够用了。并且结合Reflection机制,它的功能比VsaEngine更强大:它可以编译任何提供CompilerProvider的CLR语言(目前.NET自带的语言中都有)。当然,它也有一些缺点:它生成的Assembly不能动态卸载。这在一般情况下不成问题,因为一个源代码只需编译一次,并载入执行,并不需要动态卸载。假如你需要做脚本编辑器时,就要考虑这个问题,因为有可能一个脚本会因为修修改改而不停的重新编译,从而造成不停的产生新的Assembly,最后将导致内存被大量占用。要解决这个问题,需要将编译器加载到独立的AppDomain中,通过卸载AppDomain达到卸载所需的Assembly的目的。


网站题目:vb.net脚本 vbnet web编程
文章分享:http://cdkjz.cn/article/hppige.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220