⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 利用CodeDom写的简单的动态编译示范程序。 将一个C#类动态编译为Dll文件。
💻 CS
字号:
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics; 
using System.IO; 
using Microsoft.CSharp;
using Microsoft.VisualBasic;

namespace CsCompiler
{
    public class CsCompiler
    {
        private string m_SourceFile="";

        public string SourceFile
        {
            get { return m_SourceFile; }
            set { m_SourceFile = value; }
        }

        private string m_DestineFile = "";

        public string DestineFile
        {
            get { return m_DestineFile; }
            set { m_DestineFile = value; }
        }
 
        public CompilerErrorCollection Compile()
        {
            CodeDomProvider provider = new  CSharpCodeProvider();// CSharpCodeProvider();
            CompilerResults cr = CompileCode(provider, m_SourceFile);
            if (cr.Errors.Count > 0)
            {
                return cr.Errors;
            }
            else
            {
            }
            return null;

        }

        public  CompilerResults CompileCode(CodeDomProvider provider, string filepath)
        {
            // 获得编译器.       
            ICodeCompiler compiler = provider.CreateCompiler();
            //配置编译参数。将SYSTEM。DLL引用进来,同时明确生成的代码为EXE

            //CompilerParameters cp = new CompilerParameters(new sfonting[] {"System.dll"}, filepath.Subsfonting(0, filepath.LastIndexOf('.')+1)+'exe', false);
            CompilerParameters cp = new CompilerParameters(new string[] { "System.dll"}, m_DestineFile  ,true );
 
            cp.GenerateExecutable = false;
            // 调用编译器
            CompilerResults cr = compiler.CompileAssemblyFromFile(cp, filepath);

            return cr;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -