C#动态编译dll或exe
2024-08-30 07:53:40
string strCode = @"
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Reflection; namespace aaaa
{
public class Program
{
static void Main(string[] args)
{
GetDoc(""Testdoc.doc"");
Console.WriteLine(""输出成功"");
Console.ReadLine();
} public static void GetDoc(string name)
{
try
{
Assembly ass = Assembly.GetExecutingAssembly();
Stream ss = ass.GetManifestResourceStream(name);
if (ss != null)
{
byte[] buffer = new byte[ss.Length];
ss.Read(buffer, 0, buffer.Length);
File.WriteAllBytes(Environment.CurrentDirectory + ""\\ProbeDoc.doc"", buffer);
}
}
catch
{
Console.WriteLine(""error"");
}
}
}
}"; CompilerParameters objCompilerParams = new CompilerParameters();
objCompilerParams.GenerateExecutable = true; //编译成exe还是dll
objCompilerParams.GenerateInMemory = false; //是否写入内存,不写入内存就写入磁盘
objCompilerParams.OutputAssembly = "F:\\abcd.exe"; //输出路径
objCompilerParams.IncludeDebugInformation = false; //是否产生pdb调试文件 默认是false
objCompilerParams.ReferencedAssemblies.Add("System.dll");
objCompilerParams.ReferencedAssemblies.Add("System.Core.dll");
objCompilerParams.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
objCompilerParams.EmbeddedResources.Add("D:\\Testdoc.doc"); //编译器选项:编译成(存储在内存中)的DLL
/*objCompilerParams.CompilerOptions = "/target:library /optimize";
//编译时在内存输出
objCompilerParams.GenerateInMemory = true;
//不生成调试信息
objCompilerParams.IncludeDebugInformation = false;*/
//创建编译类
CSharpCodeProvider objCompiler = new CSharpCodeProvider();
//进行编译
CompilerResults objCompileResults = objCompiler.CompileAssemblyFromSource(objCompilerParams, strCode); //获取编译结果:程序集
Assembly objAssembly = objCompileResults.CompiledAssembly; ////获取编译成的程序集的信息
//object objMainClassInstance = objAssembly.CreateInstance("Program");
//Type objMainClassType = objMainClassInstance.GetType(); ////调用程序集中的类,执行类中的方法,得到结果
//objMainClassType.GetMethod("Main").Invoke(objMainClassInstance, null);
//objMainClassType.GetMethod("PrintWorld").Invoke(objMainClassInstance, null);
最新文章
- String转json
- gulp教程之gulp-minify-css
- PyCharm 3.4.1注册码
- kettle之mongodb数据同步
- Google Play笔记之上架
- 夺命雷公狗---DEDECMS----19dedecms栏目列表页的完成
- [Java 并发] Java并发编程实践 思维导图 - 第二章 线程安全性
- Windows下为Python编译C扩展模块
- HTML/CSS/JavaScript学习总结(转)
- Git版本管理荟萃
- Cognos 11.0快速开发指南 Ⅰ
- centos7防火墙设置
- XML外部实体注入漏洞(XXE)
- 接入层高性能缓存技术nginx+redis利器OpenResty
- keepalived主备节点都配置vip,vip切换异常案例分析
- python Trie树和双数组TRIE树的实现. 拥有3个功能:插入,删除,给前缀智能找到所有能匹配的单词
- CSS特效(弧光效果)
- cogs1772 [国家集训队2010]小Z的袜子
- [Golang学习笔记] 05 程序实体2 作用域访问权限和变量重声明
- 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory
热门文章
- javascript图片放大镜效果展示
- Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) A. The King's Race
- kb-07线段树--10--dfs序建树
- bzoj 3924 幻想乡战略游戏
- 【leetcode】Find All Anagrams in a String
- 数据结构自己实现——stack
- AC日记——Dylans loves tree hdu 5274
- Hibernate游记——装备篇《一》(基础配置详解)
- nfs详解及实现全网备份
- 济南day6