C#动态编译dll或exe

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);

 

转载于:https://www.cnblogs.com/a849788087/p/6689180.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值