GenerateInMemory用于指示是否在内存在生成Assembly
GenerateExecutable用于指示生成的Assembly类型是exe还是dll
【vb.net脚本引擎 vb脚本是什么意思】OutputAssembly用于指示生成的程序文件名(仅在GenerateInMemory为false的情况)
ReferencedAssemblies用于添加引用Assembly
例如:
theParameters.ReferencedAssemblies.Add("System.dll");
创建指定语言的编译器
编译需要由指定语言的CodeDomProvider生成 。
这里列举一些.NET的CodeDomProvider:
vb.netMicrosoft.VisualBasic.VBCodeProvider
C#Microsoft.CSharp.CSharpCodeProvider
jscriptMicrosoft.JScript.JScriptCodeProvider
J#Microsoft.VJSharp.VJSharpCodeProvider
以C#为例 , 要创建C#编译器,代码如下:
//.NET 1.1/1.0
ICodeCompiler compiler = new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();
//.NET 2.0
ICodeCompiler compiler = (ICodeCompiler) new Microsoft.CSharp.CSharpCodeProvider();
下面是完整的创建编译器的例子:
/// summary
/// 创建相应脚本语言的编译器
/// /summary
private void createCompiler(string strLanguage, bool debugMode, string strAssemblyFileName)
{
this.theParameters = new CompilerParameters();
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();
CodeDomProvider theProvider;
if("visualbasic" == strLanguage || "vb" == strLanguage)
{
theProvider = new Microsoft.VisualBasic.VBCodeProvider();
if(debugMode)
theParameters.CompilerOptions += "/debug:full /optimize- /optionexplicit+ /optionstrict+ /optioncompare:text /imports:Microsoft.VisualBasic,System,System.Collections,System.Diagnostics ";
else
theParameters.CompilerOptions += "/optimize /optionexplicit+ /optionstrict+ /optioncompare:text /imports:Microsoft.VisualBasic,System,System.Collections,System.Diagnostics ";
AddReference("Microsoft.VisualBasic.dll");
}
else if("jscript" == strLanguage || "js" == strLanguage)
{
theProvider = new Microsoft.JScript.JScriptCodeProvider();
AddReference("Microsoft.JScript.dll");
}
else if("csharp" == strLanguage || "cs" == strLanguage || "c#" == strLanguage)
{
theProvider = new Microsoft.CSharp.CSharpCodeProvider();
if(!debugMode)
theParameters.CompilerOptions += "/optimize ";
}
//else if("jsharp" == strLanguage || "vj" == strLanguage || "j#" == strLanguage)
//{
//theProvider = new Microsoft.VJSharp.VJSharpCodeProvider();
//if(!debugMode)
//theParameters.CompilerOptions += "/optimize ";
//}
else
throw new System.Exception("指定的脚本语言不被支持 。");
this.theCompiler = theProvider.CreateCompiler();
}
/// summary
/// 添加引用对象 。
/// /summary
/// param name="__strAssemblyName"引用的文件名/param
public void AddReference(string __strAssemblyName)
{
theParameters.ReferencedAssemblies.Add(__strAssemblyName);
}
注:
在.NET Framework 2.0中,由于CreateCompiler方法被标记作废 。为避免产生编译警告,可直接返回CodeDomProvider作为编译器:
this.theCompiler = (ICodeCompiler)theProvider;
推荐阅读
- 奥特曼格斗6游戏,奥特曼格斗6游戏怎么玩
- .net开发教程pdf下载地址,net 开发
- 三琪药房公众号关注,三奇药业
- qq安卓5g在线可以调吗,安卓怎么5g在线
- c语言怎么计算ln函数 c语言ln函数怎么写
- 出来chatgpt学雅思,考完雅思出国就能听懂吗
- 投诉短视频内容是什么,投诉短视频平台去哪个部门
- ppt如何做模糊效果,ppt如何模糊图片内容
- 单独安装VB.NET 单独安装下水道多少钱