扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:小刀人编译 来源:VCKBASE 2007年11月3日
关键字:
static Assembly CompileInMemory(string code) { CSharpCodeProvider provider = new CSharpCodeProvider(); CompilerParameters options = new CompilerParameters(); options.IncludeDebugInformation = false; options.GenerateExecutable = false; options.GenerateInMemory = true; CompilerResults results =provider.CompileAssemblyFromSource(options, code); provider.Dispose(); Assembly generatedAssembly = null; if (results.Errors.Count == 0) { generatedAssembly = results.CompiledAssembly; } return generatedAssembly; } |
using System; using System.Collections; using System.Reflection; namespace CodeGuru.CodeDomSample { class ExecutionHost { private Hashtable assemblies = null; public ExecutionHost() { assemblies = new Hashtable(); // 响应类型解析事件(the type resolution event)要求以截取它并找到我们类型 AppDomain.CurrentDomain.TypeResolve += new ResolveEventHandler(CurrentDomain_TypeResolve); } private Assembly CurrentDomain_TypeResolve(object sender,ResolveEventArgs args) { // 为预定的类型找出我们程序集 Assembly a = null; if (assemblies.ContainsKey(args.Name)) { a = (Assembly)assemblies[args.Name]; } return a; } public void AddAssembly(string fullTypeName, Assembly a) { assemblies.Add(fullTypeName, a); } public string Execute(string typeFullName, string msg) { // 尝试创建触发事件所需要的类型 Type targetType = Type.GetType(typeFullName, true, true); object target =targetType.Assembly.CreateInstance(typeFullName); IExecutableModule m = (IExecutableModule)target; return m.SayHello(msg); } } } namespace CodeGuru.CodeDomSample { public interface IExecutableModule { string SayHello(string inputMessage); } } public static CodeCompileUnit CreateExecutionClass(string typeNamespace,string typeName,string scriptBody) { // 创建CodeCompileUnit以存放代码 CodeCompileUnit ccu = new CodeCompileUnit(); // 分配给预期的命名空间 CodeNamespace cns = new CodeNamespace(typeNamespace); cns.Imports.Add(new CodeNamespaceImport("System")); ccu.Namespaces.Add(cns); // 创建类 CodeTypeDeclaration parentClass = new CodeTypeDeclaration(typeName); cns.Types.Add(parentClass); // 新行-为IExecutableModule接口添加一个实现 parentClass.BaseTypes.Add(typeof(CodeGuru.CodeDomSample.IExecutableModule)); // 创建获得一个参数并返回一个字符串的SayHello方法 CodeMemberMethod method = new CodeMemberMethod(); method.Name = "SayHello"; method.Attributes = MemberAttributes.Public; CodeParameterDeclarationExpression arg = new CodeParameterDeclarationExpression(typeof(string), "inputMessage"); method.Parameters.Add(arg); method.ReturnType = new CodeTypeReference(typeof(string)); // 添加预期代码到方法实体 CodeSnippetStatement methodBody = new CodeSnippetStatement(scriptBody); method.Statements.Add(methodBody); parentClass.Members.Add(method); return ccu; } |
static Assembly CompileInMemory(string code) { CSharpCodeProvider provider = new CSharpCodeProvider(); CompilerParameters options = new CompilerParameters(); options.IncludeDebugInformation = false; options.GenerateExecutable = false; options.GenerateInMemory = true; // 新行-添加一个接口到需要的程序集 options.ReferencedAssemblies.Add("CodeGuru.CodeDomSample.exe"); CompilerResults results = provider.CompileAssemblyFromSource(options, code); provider.Dispose(); Assembly generatedAssembly = null; if (results.Errors.Count == 0) { generatedAssembly = results.CompiledAssembly; } return generatedAssembly; } |
string typeNamespace = "CodeGuru.DynamicCode"; string typeName = "ScriptType" + Guid.NewGuid().ToString("N"); Assembly a = CompileInMemory(GenerateCode(typeNamespace, typeName,"return inputMessage;")); ExecutionHost host = new ExecutionHost(); string fullTypeName = typeNamespace + "." + typeName; host.AddAssembly(fullTypeName, a); string test = host.Execute(fullTypeName, "Hello World!"); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者