Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图以编程方式编译和执行C#代码_C# - Fatal编程技术网

试图以编程方式编译和执行C#代码

试图以编程方式编译和执行C#代码,c#,C#,以下是我的代码: using System; using System.Collections.Generic; using System.Text; using System.CodeDom.Compiler; using System.IO; using Microsoft.CSharp; using System.Reflection; namespace DynaCode { class Program { static void Main(string[] args)

以下是我的代码:

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

using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;

namespace DynaCode
{

class Program
{

    static void Main(string[] args)
    {

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


        CompileAndRun(code);

        Console.ReadKey();

    }

    static void CompileAndRun(string[] code)
    {
        CompilerParameters CompilerParams = new CompilerParameters();
        string outputDirectory = Directory.GetCurrentDirectory();

        CompilerParams.GenerateInMemory = true;
        CompilerParams.TreatWarningsAsErrors = false;
        CompilerParams.GenerateExecutable = false;
        CompilerParams.CompilerOptions = "/optimize";

        string[] references = { "System.dll"};

        CompilerParams.ReferencedAssemblies.AddRange(references);

        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, code);

        if (compile.Errors.HasErrors)
        {
            string text = "Compile error: ";
            foreach (CompilerError ce in compile.Errors)
            {
                text += "rn" + ce.ToString();
            }
            throw new Exception(text);
        }

        //ExpoloreAssembly(compile.CompiledAssembly);

        Module module = compile.CompiledAssembly.GetModules()[0];
        Type mt = null;
        MethodInfo methInfo = null;

        if (module != null)
        {
            mt = module.GetType("DynaCore.DynaCore");
        }

        if (mt != null)
        {
            methInfo = mt.GetMethod("Main");
        }

        if (methInfo != null)
        {
            Console.WriteLine(methInfo.Invoke(null, new object[] { "here in dyna code" }));
        }
    }

    static void ExpoloreAssembly(Assembly assembly)
    {
        Console.WriteLine("Modules in the assembly:");
        foreach (Module m in assembly.GetModules())
        {
            Console.WriteLine("{0}", m);

            foreach (Type t in m.GetTypes())
            {
                Console.WriteLine("t{0}", t.Name);

                foreach (MethodInfo mi in t.GetMethods())
                {
                    Console.WriteLine("tt{0}", mi.Name);
                }
            }
        }
    }
}
}
hi.cs文件的内容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;



namespace DynaCore
{
class DynaCore
{
    static public void Main(string[] args)
    {

     Console.WriteLine("hello, this is good");

    }
}
}
这是我尝试运行此程序时遇到的错误:

System.Exception was unhandled
  Message="Compile error: rnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.16.cs(1,19) : error CS1514: { expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.16.cs(1,19) : error CS1513: } expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.18.cs(1,1) : error CS0116: A namespace does not directly contain members such as fields or methodsrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.20.cs(1,19) : error CS1514: { expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.20.cs(1,19) : error CS1513: } expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.20.cs(1,11) : error CS0101: The namespace '<global namespace>' already contains a definition for 'DynaCore'rnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.22.cs(1,5) : error CS0116: A namespace does not directly contain members such as fields or methodsrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.24.cs(1,23) : error CS1518: Expected class, delegate, enum, interface, or structrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.24.cs(1,40) : error CS1001: Identifier expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.24.cs(1,42) : error CS1518: Expected class, delegate, enum, interface, or structrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.26.cs(1,9) : error CS0116: A namespace does not directly contain members such as fields or methodsrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.30.cs(1,10) : error CS0116: A namespace does not directly contain members such as fields or methodsrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.35.cs(1,9) : error CS1022: Type or namespace definition, or end-of-file expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.37.cs(1,5) : error CS1022: Type or namespace definition, or end-of-file expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.39.cs(1,1) : error CS1022: Type or namespace definition, or end-of-file expected"
  Source="Compiling"
  StackTrace:
       at DynaCode.Program.CompileAndRun(String[] code) in C:\Documents and Settings\simonjef\My Documents\Visual Studio 2008\Projects\Compiling\Compiling\Program.cs:line 72
       at DynaCode.Program.Main(String[] args) in C:\Documents and Settings\simonjef\My Documents\Visual Studio 2008\Projects\Compiling\Compiling\Program.cs:line 42
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
System.Exception未处理
Message=“编译错误:rnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.16.cs(1,19):错误CS1514:{expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.16.cs(1,19):错误CS1513:}expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.18.cs(1,1):错误CS0116:名称空间不直接包含字段或方法等成员RNC:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.20.cs(1,19):错误CS1514:{expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.20.cs(1,19):错误CS1513:}expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.20.cs(1,11):错误CS0101:命名空间“”已包含“DynaCore”rnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.22.cs(1,5)的定义:错误CS0116:命名空间不直接包含字段或方法等成员NC:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.24.cs(1,23):错误CS1518:预期的类、委托、枚举、接口或结构RC:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.24.cs(1,40):错误CS1001:标识符expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.24.cs(1,42):错误CS1518:预期的类、委托、枚举、接口或结构RNC:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.26.cs(1,9):错误CS0116:命名空间不直接包含字段或方法等成员NC:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.30.cs(1,10):错误CS0116:命名空间不直接包含字段或方法等成员NC:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.35.cs(1,9):错误CS1022:类型或命名空间定义,或文件结尾expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.37.cs(1,5):错误CS1022:类型或命名空间定义,或文件结尾expectedrnc:\\Documents and Settings\\simonjef\\Local Settings\\Temp\\mftbafdt.39.cs(1,1):错误CS1022:类型或命名空间定义,或应为文件结尾“
Source=“编译”
堆栈跟踪:
在C:\Documents and Settings\simonjef\My Documents\Visual Studio 2008\Projects\Compile\CompileAndRun(字符串[]代码)中的DynaCode.Program.CompileAndRun(字符串[]代码)处:第72行
在C:\Documents and Settings\simonjef\My Documents\Visual Studio 2008\Projects\Compiling\Compiling\Program.cs中的DynaCode.Program.Main(字符串[]args)处:第42行
位于System.AppDomain.\u nexecutestAssembly(程序集,字符串[]args)
位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
内部异常:

我认为这就是问题所在:

string content = File.ReadAllText(@"D:\hi.cs");
string[] code = new string[content.Length];
char[] seperators = { '\n','\r','\t' };
code = content.Split(seperators);
这个想法(我相信)是不需要单独的行-数组中的每个字符串都是一个完整的C#源文件。因此,您可能只需要:

请注意,即使您的第一个块所做的事情是正确的,您仍然会无缘无故地创建一个字符串数组-将其编写为:

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

您不需要将代码拆分成这样的数组


该数组用于传递多个源文件。

您可以尝试这样编写代码

                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" }));

                }

而且它在我的代码中没有任何错误

为什么要将源代码拆分为多个字符串?在我的机器上编译和执行该方法几乎需要90到100毫秒。如果您试图在现有代码执行中动态推入功能,也可以使用MEF。将是比使用核心反射和编译器进程更好的选择
                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" }));

                }