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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - 血狼

切换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);
                }

            }

        }

    }

}