惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
罗磊的独立博客
博客园 - 司徒正美
Last Week in AI
Last Week in AI
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
B
Blog
博客园 - 【当耐特】
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 血狼

切换ThinkPad Fn键 window.event.keycode值大全 通用分页存储过程 Oracle中将密码有效期由默认的180天修改成“无限制” C#程序中执行script .Net Reactor 加密的简单例子 MSBuild的相关操作 程序中执行cmd.exe 使用c#捕获Windows的关机事件 北京嘉纳博科技有限公司 Windows Service 初始化代码中追加断点问题 C#定时执行某个程序 .Net中关于多个FrameWork的问题 Chilkat 收、送信 合并文件 设置和获取注册表数据 DotNet多个程序集合并工具 PostGres 全文检索 PostGres 中包含触发器的方法
C#中动态编译某C#文件
血狼 · 2008-10-24 · via 博客园 - 血狼

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;

namespace news.newsscript.impl
{
    
/// <summary>
    
/// Summary description for NewsCSharpScriptExcuter.
    
/// </summary>

    public class NewsCSharpScriptExcuter:AbstractNewsScriptExcuter
    
{
        
public NewsCSharpScriptExcuter()
        
{
            
//
            
// TODO: Add constructor logic here
            
//
        }


        
private CodeDomProvider provider;

        
private ICodeCompiler compiler;
        
        
private CompilerResults results;
        
        
private CompilerParameters compilerparams;

        
public override object RunNewsScript(object param)
        
{
            
return RunCSharpScript(Path,param);
        }



        
private object RunCSharpScript(string directory ,object param)
        
{
            DirectoryInfo directoryInfo 
=new DirectoryInfo(directory);
            provider 
= new Microsoft.CSharp.CSharpCodeProvider();
            compiler
= provider.CreateCompiler();
            compilerparams 
= new CompilerParameters();
            
//compilerparams.GenerateInMemory = true;
            compilerparams.ReferencedAssemblies.Add("System.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Windows.Forms.Dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Configuration.Install.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Data.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Design.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.DirectoryServices.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Drawing.Design.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Drawing.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.EnterpriseServices.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Management.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Runtime.Remoting.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Messaging.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.Security.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.ServiceProcess.dll");
            compilerparams.ReferencedAssemblies.Add(
"System.XML.dll");
            compilerparams.GenerateExecutable 
= true;
            results 
= compiler.CompileAssemblyFromFileBatch(compilerparams,GetFileList(directoryInfo));
            
if (results.Errors.HasErrors)
            
{
                StringBuilder errors 
= new StringBuilder();
                
foreach(System.CodeDom.Compiler.CompilerError error in results.Errors)
                
{
                    errors.Append(error.ErrorText 
+ "\r\n");                    
                }

                
throw new NewsScriptCompileException(errors.ToString());
            }

            
else
            
{
                
try
                
{
                    Assembly generatedAssembly 
= results.CompiledAssembly;
                    
object obj = null;
                    
if (string.IsNullOrEmpty(this.ProcedureName)) {
                        obj 
= CallEntry(generatedAssembly, "NewsMainScript", param);
                    }
 else {
                        obj 
= CallEntry(generatedAssembly, ProcedureName, param);
                    }

                    
return obj;
                }

                
catch(Exception ex)
                
{
                    
throw new NewsScriptRuntimeException(ex.Message,ex);
                }

            }

        }

    }

}