How can I get standard out for an assembly via reflection?
More or less, I am dynamically executing开发者_如何学Python user code, and I would like to capture things like Write and Read methods from console, like a file or command line program would.
Here's what my code looks like for executing a dynamic program with one Main.
_assembly = compilerResults.CompiledAssembly;
object o = _assembly.CreateInstance(ClassNameString);
MethodInfo mi = _assembly.EntryPoint;
mi.Invoke(o, null);
An assembly doesn't get a separate output. That's per process.
You'll have to listen to your own standard out or pass a stream into your generated class.
You can set the reader and writer behind Console.Out and Console.In:
Console.SetOut(new MyInterceptingTextWriter());
Console.SetIn(new MyInterceptingReader());
// your code here
精彩评论