assemblygen.cs

来自「语音视频功能 里面实现了基本的QQ与语音对话」· CS 代码 · 共 71 行

CS
71
字号
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Security;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace gowk.GXF
{
	/// <summary>
	/// AssemblyGen 的摘要说明。
	/// </summary>
	internal class AssemblyGen
	{
		public AssemblyGen()
		{
		}
		public Assembly CreateAssembly(Script script)
		{
			//create the language specific code compiler
			ICodeCompiler compiler = CreateCompiler(script);

			//add compiler parameters
			CompilerParameters compilerParams = new CompilerParameters();
			compilerParams.CompilerOptions = "/target:library /optimize";
			compilerParams.GenerateExecutable = false;
			compilerParams.GenerateInMemory = false;			
			compilerParams.IncludeDebugInformation = true;
			compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
			compilerParams.ReferencedAssemblies.Add("System.dll");
			compilerParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");	
			compilerParams.ReferencedAssemblies.Add("System.Xml.dll");	
			compilerParams.ReferencedAssemblies.Add("System.Data.dll");	
			foreach(string reference in script.References)
			{
				compilerParams.ReferencedAssemblies.Add(reference);
			}
			CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, script.SourceCode);
			if(results.Errors.Count>0)
			{
				string reason="";
				foreach(System.CodeDom.Compiler.CompilerError er in results.Errors)
				{
					reason+="ERROR:\n";
					reason+=er.ErrorText+"\n";
					reason+="@"+"line:"+er.Line.ToString()+",column:"+er.Column.ToString();
					reason+="\n";
				}
				throw new System.Exception(reason);return null;
			}
			return results.CompiledAssembly;
		}
		private ICodeCompiler CreateCompiler(Script script)
		{
			return new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();
		}
	}
	internal class Script
	{
		public string Language;
		public string SourceCode;
		public ArrayList References=new ArrayList();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?