trying to compile and execute C# code programmatically

前端 未结 3 410
悲&欢浪女
悲&欢浪女 2021-01-14 20:46

The following is my code :

using System;
using System.Collections.Generic;
using System.Text;

using System.CodeDom.Compiler;
using System.IO;
using Microsof         


        
相关标签:
3条回答
  • 2021-01-14 21:24

    you dont need to split your code into an array like that.

    the array is for passing multiple source files.

    0 讨论(0)
  • 2021-01-14 21:31

    you can try to make code like this

                    string content = File.ReadAllText(@Path.GetDirectoryName(Application.ExecutablePath) + "\\r.cs");
    
                    char[] seperators = { '\n', '\r', '\t' };
                    string[] code = content.Split(seperators);
    
                    CompilerParameters CompilerParams = new CompilerParameters();
                    string outputDirectory = Directory.GetCurrentDirectory();
    
                    CompilerParams.GenerateInMemory = true;
                    CompilerParams.TreatWarningsAsErrors = false;
                    CompilerParams.GenerateExecutable = false;
                    CompilerParams.CompilerOptions = "/optimize";
                    CompilerParams.GenerateExecutable = false;
                    CompilerParams.OutputAssembly = "r.dll";
                    FileStream fileStream = File.Open(Path.GetDirectoryName(Application.ExecutablePath) + "\\Output\\r.dll",FileMode.Open);
                    string references =  fileStream.Name;
    
    
    
    
                    CompilerParams.ReferencedAssemblies.Add(references);
                    fileStream.Close();
                    CSharpCodeProvider provider = new CSharpCodeProvider();
                    CompilerResults compile = provider.CompileAssemblyFromFile(CompilerParams, Path.GetDirectoryName(Application.ExecutablePath) + "\\r.cs");
    
                    if (compile.Errors.HasErrors)
                    {
                        string text = "Compile error: ";
                        foreach (CompilerError ce in compile.Errors)
                        {
                            text += "rn" + ce.ToString();
                        }
                        throw new Exception(text);
                    }
    
                    Module module = compile.CompiledAssembly.GetModules()[0];
                    Type mt = null;
                    MethodInfo methInfo = null;
                    MemberInfo[] memberInfo;
                    //var dll = Assembly.LoadFile(references);
    
                    if (module != null)
                    {
                        mt = module.GetType("materialclassifier.ClassifierFiles");
                    }
    
                    if (mt != null)
                    {
                        memberInfo = mt.GetMembers();
                        methInfo = mt.GetMethod("main");
                    }
    
                    if (methInfo != null)
                    {
                        Console.WriteLine(methInfo.Invoke(null, new object[] { "here in dyna code" }));
    
                    }
    

    and it is working without any error in my code

    0 讨论(0)
  • 2021-01-14 21:35

    I believe this is the problem:

    string content = File.ReadAllText(@"D:\hi.cs");
    string[] code = new string[content.Length];
    char[] seperators = { '\n','\r','\t' };
    code = content.Split(seperators);
    

    The idea (I believe) is that CompileAssemblyFromSource doesn't take individual lines - each string in the array is meant to be a complete C# source file. So you probably just need:

    string[] code = new[] { File.ReadAllText(@"D:\hi.cs") };
    

    Note that even if your first block were doing the right thing, you'd still have been creating a string array for no reason - it would have been simpler to write it as:

    string content = File.ReadAllText(@"D:\hi.cs");
    char[] seperators = { '\n','\r','\t' };
    string[] code = content.Split(seperators);
    
    0 讨论(0)
提交回复
热议问题