• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#动态编译dll或exe

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#函数及out传值发布时间:2022-07-10
下一篇:
c#parallel.for怎么控制并发数量,控制多5个并发量发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap