I want to get the stdout of a dynamically compiled code.
My code:
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var source = File.ReadAllText("form.cs");
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v4.0"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
ReferencedAssemblies = {"System.dll" ,"mscorlib.dll"}
};
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("program.TestPerson");
MethodInfo main = program.GetMethod("Main");
var outp= main.Invoke(null, null);
//Console.WriteLine(outp);
Console.ReadLine();
}
}
}
The content of form.cs:
using System;
namespace program {
public class TestPerson
{
public static void Main()
{
var person1 = new Person();
Console.WriteLine(person1.Name);
}
}
}
public class Person
{
public Person()
{
Name = "unknown";
}
public Person(string name)
{
Name = name;
}
public string Name { get;set; }
public override string ToString()
{
return Name;
}
}
What I exactly want is to have the stdout of form.cs (Console.WriteLine) after compilation in a variable in parent applicaton, by the way, I do NOT want to build the code into the file and run it as process and read its output. Also assume the content of form.cs is NOT editable.
The main
might have been confused you, but as I wrote in my comment, your dynamically compiled code does not run in its own process (that can be also achieved, but it is far more complicated), thus it does not have its own output. The method main
is just another method in just another class in your default AppDomain in your current process. This means it will write to the console of your outer, hosting process. You will have to capture that output with Console.SetOut
. See following linqpad snippet:
string source = @"using System;
namespace program {
public class TestPerson
{
public static void Main()
{
Console.WriteLine(""TEST"");
}
}
}";
void Main()
{
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v4.0"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
ReferencedAssemblies = { "System.dll", "mscorlib.dll" }
};
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("program.TestPerson");
MethodInfo main = program.GetMethod("Main");
var sb = new StringBuilder();
var writer = new StringWriter(sb);
Console.SetOut(writer);
var outp = main.Invoke(null, null);
sb.ToString().Dump(); // this Dump is from linqpad, do what you want with the StringBuilder content
Console.ReadLine();
}
If you want to write to the original standard output, save it first, like this:
...
var oldOut = Console.Out;
Console.SetOut(writer);
var outp = main.Invoke(null, null);
oldOut.WriteLine($"The result is: {sb.ToString()}");