如何在运行时在C#Dot Net中编译代码?

时间:2017-04-30 10:09:02

标签: c# compilation

所以我想编译一个.cs文件的整个文件夹,然后创建一个DLL文件,然后在运行时在我的项目中使用该DLL。

我搜索了互联网,发现CSharpCodeProvider可以帮助我。

但令我困惑的是,该网站上的大多数示例都展示了如何读取单个文件,而不是整个文件夹。

所以我假设我的包含.cs文件的文件夹将链接在一起。 示例文件:

文件: TestMain.cs

class TestMain
    {
        public static void Main(string[] args)
        {
            Test t = new Test();
            t.Hello();
        }
    }

文件: Test.cs

public class Test
    {
        public void Hello()
        {
            Console.Write(@"Hello");
        }
    }

任何指导都将受到赞赏。

3 个答案:

答案 0 :(得分:1)

好的,所以在搜索和指导后,这是我的工作代码:

public static Assembly CompileAssembly(string[] sourceFiles, string outputAssemblyPath)
        {
            var codeProvider = new CSharpCodeProvider();

            var compilerParameters = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory = false,
                OutputAssembly = outputAssemblyPath
            };

            // Add CSharpSimpleScripting.exe as a reference to Scripts.dll to expose interfaces
            //compilerParameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);

            var result = codeProvider.CompileAssemblyFromFile(compilerParameters, sourceFiles); // Compile
            if (result.Errors.Count > 0)
            {
                MessageBox.Show(@"Error Occured");
            }
            else
            {
                return result.CompiledAssembly;

            }
            return null;
        }

答案 1 :(得分:0)

string sourceCode = @"
    public class Test
    {
       public void Hello()
       {
          Console.Write(@'Hello');
       }
    }";

    var compParms = new CompilerParameters{
        GenerateExecutable = false, 
        GenerateInMemory = true
    };

    var csProvider = new CSharpCodeProvider();
    CompilerResults compilerResults = 
            csProvider.CompileAssemblyFromSource(compParms, sourceCode);
    object typeInstance = 
            compilerResults.CompiledAssembly.CreateInstance("Test");
    MethodInfo mi = typeInstance.GetType().GetMethod("Hello");
    mi.Invoke(typeInstance, null); 
    Console.ReadLine();

答案 2 :(得分:0)

基本上,您可以使用CodeDom.Compiler来编译dll,我写了类似的东西,然后使用Reflection稍后动态引用它

//dot net compiler
using System; 
using System.CodeDom.Compiler; 
using System.IO; 

namespace IndiLogix.dotCompiler 
{ 
    class dotCompiler 
    { 
        FileInfo sourceFile;// = new FileInfo(sourceName); 
        CodeDomProvider provider = null; 
        bool compileOk = false; 


        // Compile Executable 
        public bool CompileExecutable(String sourceName) 
        { 
            sourceFile = new FileInfo(sourceName); 
            I_GetProvider(sourceFile); 
            if (sourceFile.Extension.ToUpper(System.Globalization.CultureInfo.InvariantCulture) == ".CS") 
            { 
                provider = CodeDomProvider.CreateProvider("CSharp"); 
                //return "CSharp"; 
            } 

            if (provider != null) 
            { 

                // Format the executable file name. 
                // Build the output assembly path using the current directory 
                // and _cs.exe or _vb.exe. 

                String exeName = String.Format(@"{0}\{1}.exe", 
                System.Environment.CurrentDirectory, 
                sourceFile.Name.Replace(".", "_")); 
                string dllName = String.Format(@"{0}\{1}.dll", System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_")); 

                CompilerParameters cp = new CompilerParameters(); 

                // Generate an executable instead of a class library. 
                cp.GenerateExecutable = true; 

                // Specify the assembly file name to generate. 
                cp.OutputAssembly = exeName; 

                // Save the assembly as a physical file. 
                cp.GenerateInMemory = false; 

                // Set whether to treat all warnings as errors. 
                cp.TreatWarningsAsErrors = false; 

                // Invoke compilation of the source file. 
                CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName); 
                string temp; 
                if (cr.Errors.Count > 0) 
                { 
                    // Display compilation errors. 
                    temp = sourceName + "\n" + cr.PathToAssembly; 

                    foreach (CompilerError ce in cr.Errors) 
                    { 
                        temp += "\nError:" + ce.ToString(); 
                    } 
                    System.Windows.Forms.MessageBox.Show(temp, "dotCompiler Error:", System.Windows.Forms.MessageBoxButtons.OK); 
                } 
                else 
                { 
                    // Display a successful compilation message. 
                    //Console.WriteLine("Source {0} built into {1} successfully.",sourceName, cr.PathToAssembly); 
                    System.Windows.Forms.MessageBox.Show("Solution build sucessfully..\n\n" + sourceName + "\n" + cr.PathToAssembly,"dotCompiler:)",System.Windows.Forms.MessageBoxButtons.OK); 
                } 

                // Return the results of the compilation. 
                if (cr.Errors.Count > 0) 
                { 
                    compileOk = false; 
                } 
                else 
                { 
                    compileOk = true; 
                } 
            } 
            return compileOk; 
        } 

        private void I_GetProvider(FileInfo sourceFile) 
        { 
            // Select the code provider based on the input file extension. 
            if (sourceFile.Extension.ToUpper(System.Globalization.CultureInfo.InvariantCulture) == ".CS") 
            { 
                provider = CodeDomProvider.CreateProvider("CSharp"); 
            } 
            else if (sourceFile.Extension.ToUpper(System.Globalization.CultureInfo.InvariantCulture) == ".VB") 
            { 
                provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("VisualBasic"); 
            } 
            else 
            { 
                //Console.WriteLine("Source file must have a .cs or .vb extension"); 
                //_Notify("Error:", "Source file must have a .cs or .vb extension", ToolTipIcon.Error); 
                System.Windows.Forms.MessageBox.Show( 
                "Source file must have *.cs or *.vb extension", "dotCompiler Error", 
                System.Windows.Forms.MessageBoxButtons.OK); 
            } 
        } 

        private string I_GetProvider_RetStr(FileInfo sourceFile) 
        { 

            // Select the code provider based on the input file extension. 
            if (sourceFile.Extension.ToUpper(System.Globalization.CultureInfo.InvariantCulture) == ".CS") 
            { 
                provider = CodeDomProvider.CreateProvider("CSharp"); 
                return "CSharp"; 
            } 
            else if (sourceFile.Extension.ToUpper(System.Globalization.CultureInfo.InvariantCulture) == ".VB") 
            { 
                provider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("VisualBasic"); 
                return "VisualBasic"; 
            } 
            else 
            { 
                //Console.WriteLine("Source file must have a .cs or .vb extension"); 
                //_Notify("Error:", "Source file must have a .cs or .vb extension", ToolTipIcon.Error); 
                return "Source file must have *.cs or *.vb extension"; 
            } 
        } 

        public bool CompileDll(String sourceName) 
        { 

            sourceFile = new FileInfo(sourceName); 
            I_GetProvider(sourceFile); 

            if (provider != null) 
            { 

            // Format the executable file name. 
            // Build the output assembly path using the current directory 
            // and _cs.exe or _vb.exe. 

            String exeName = String.Format(@"{0}\{1}.exe", 
            System.Environment.CurrentDirectory, 
            sourceFile.Name.Replace(".", "_")); 
            string dllName = String.Format(@"{0}\{1}.dll", System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_")); 

            CompilerParameters cp = new CompilerParameters(); 

            // Generate an executable instead of a class library. 
            cp.GenerateExecutable = false; 

            // Specify the assembly file name to generate. 
            cp.OutputAssembly = dllName; 

            // Save the assembly as a physical file. 
            cp.GenerateInMemory = false; 

            // Set whether to treat all warnings as errors. 
            cp.TreatWarningsAsErrors = false; 

            // Invoke compilation of the source file. 
            CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName); 
            string temp; 
            if (cr.Errors.Count > 0) 
            { 
            // Display compilation errors. 
            temp = "compiling " + sourceName + " to " + cr.PathToAssembly; 

            foreach (CompilerError ce in cr.Errors) 
            { 
            temp += "\nError:" + ce.ToString(); 
            } 

                System.Windows.Forms.MessageBox.Show(temp, "dotCompiler Error:", System.Windows.Forms.MessageBoxButtons.OK); 
            } 
            else 
            { 
                // Display a successful compilation message. 
                //Console.WriteLine("Source {0} built into {1} successfully.",sourceName, cr.PathToAssembly); 
                System.Windows.Forms.MessageBox.Show("Solution build sucessfully..\n\n" + sourceName + "\n" + cr.PathToAssembly, "dotCompiler:)", System.Windows.Forms.MessageBoxButtons.OK); 
            } 

                // Return the results of the compilation. 
                if (cr.Errors.Count > 0) 
                { 
                    compileOk = false; 
                } 
                else 
                { 
                    compileOk = true; 
                } 
            } 
        return compileOk; 
        } 

    } 
}