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

📄 compilerunit.cs

📁 这是用VC编写的一个关于计算器的代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Windows.Forms;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;

namespace ProgramCalculator
{
    /// <summary>
    /// 编译单元
    /// </summary>
    class CompilerUnit
    {
        /// <summary>
        /// 获取或设置 编译函数代码时引用的程序集的完整路径的集合
        /// </summary>
        public static StringCollection ReferenceAssemblyCollection = new StringCollection();

        private static ICodeCompiler compiler =  (new CSharpCodeProvider()).CreateCompiler();


        /// <summary>
        /// 编译
        /// </summary>
        /// <param name="source">源代码</param>
        /// <param name="makeExe">是否生成可执行程序</param>
        /// <param name="inMemory">是否在内存中生成,(如果true请将 outputAssembly设置为null)</param>
        /// <param name="dlls">加载的dll的完整路径</param>
        /// <param name="outputAssembly">输出程序集的完整路径(如果inMemory参数为true则请使用null)</param>
        public static CompilerResults Compile(string source, bool makeExe,bool inMemory,string[] dlls, string outputAssembly)
        {
            CompilerParameters parameters = new CompilerParameters();

           //加载dll
            if (dlls != null)
            {
                parameters.ReferencedAssemblies.AddRange(dlls);
            }
            parameters.GenerateExecutable = makeExe;
            parameters.GenerateInMemory = inMemory;
            if (!inMemory)
            {
                parameters.OutputAssembly = outputAssembly;
            }
           
            CompilerResults results = compiler.CompileAssemblyFromSource(parameters, source);

            return results;
        }


        /// <summary>
        /// 生成函数dll
        /// </summary>
        /// <param name="compileRes">CompilerResult对象</param>
        /// <returns>成功返回true</returns>
        public  static bool BuildFunctionDll(out CompilerResults compileRes)
        {
            //----------------------------------
            //获取源代码source
            //是所有代码相关文件(.us,.ch,.cf,.ct)的组合
            //----------------------------------
            string source = "";

            source += CompilerUnit.ReadFile(Function.GetPathOfUsingFile()) +
                CompilerUnit.ReadFile(Function.GetPathOfClassHeaderFile());
            DirectoryInfo dir = new DirectoryInfo(Function.DirectoryOfFunctions);
            FileInfo[] functionFiles = dir.GetFiles("*.cf");
            foreach (FileInfo file in functionFiles)
            {
                source += ReadFile(file.FullName);
            }
            source += CompilerUnit.ReadFile(Function.GetPathOfClassTailFile());

            //添加引用
            string[] dlls = new string[CompilerUnit.ReferenceAssemblyCollection.Count];
            CompilerUnit.ReferenceAssemblyCollection.CopyTo(dlls, 0);

            //输出路径
            string output = Function.GetPathOfNewFunctionDll();

            compileRes = CompilerUnit.Compile(source, false, false, dlls, output);

           
            //如果成功生成
            if (compileRes.Errors.Count == 0)
            {
                return true;
            }
      
            return false;
        }


        public static  string ReadFile(string path)
        {
            StreamReader sw = new StreamReader(path);
            try
            {
                return sw.ReadToEnd();
            }
            catch
            {
            }
            finally
            {
                sw.Close();
            }

            return "";

        }

    }
}

⌨️ 快捷键说明

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