0

我使用以下方法在运行时编译 C#:

CompilerParameters parameters = new CompilerParameters
        {
            GenerateInMemory = true,
            GenerateExecutable = true,
            IncludeDebugInformation =  true
        };


        // True - memory generation, false - external file generation
        // True - exe file generation, false - dll file generation

        var res = pro.CompileAssemblyFromSource(parameters,
            code);

        Assembly assembly = res.CompiledAssembly;
        Type program = assembly.GetType("First.Program");
        MethodInfo main = program.GetMethod("Main");
        var invoke = main?.Invoke(null, null);

res.Output是一个空列表,如果代码有Console.WriteLine(),它会被写入主应用程序的控制台;我想抓住写的东西。

4

1 回答 1

0

您还应该检查res.Errors. 如果有错误,那么它们将在那里。如果两者ErrorsOutput为空,那么您可能已经成功编译而没有任何输出。查看:cr.NativeCompilerReturnValue.ToString()

于 2017-10-21T16:40:27.663 回答