C# dynamic compiler, get standard output when compile in memory
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.
c# dynamic-compilation
|
show 4 more comments
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.
c# dynamic-compilation
1
You want to get the IL output? Or you want to catch theConsole.WriteLine
from your application that you would be calling?
– Icepickle
Jan 2 at 13:19
1
I might be wrong, but it looks you have confusion. The generated code is not a stand-alone process, it will be running as part of your currently running process, thus it won't have anstdout
of its own.
– ZorgoZ
Jan 2 at 13:22
@Icepickle, I want to catch the Console.WriteLine output in form.cs
– Emily Wong
Jan 2 at 13:22
@ZorgoZ, yes I know that, I think you didn't get my question, what I want to do is to dynamically compile a piece of code in memory and gets its output in a variable without building any file. more specific, lets assume the code that I want to compile is a standalone command line application which have main() function, and writing the output into console. but i want to compile it in memory and get what it would write in console in a variable.
– Emily Wong
Jan 2 at 13:25
Would setting the out help you?
– Icepickle
Jan 2 at 13:26
|
show 4 more comments
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.
c# dynamic-compilation
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.
c# dynamic-compilation
c# dynamic-compilation
edited Jan 2 at 13:39
Emily Wong
asked Jan 2 at 13:17


Emily WongEmily Wong
538
538
1
You want to get the IL output? Or you want to catch theConsole.WriteLine
from your application that you would be calling?
– Icepickle
Jan 2 at 13:19
1
I might be wrong, but it looks you have confusion. The generated code is not a stand-alone process, it will be running as part of your currently running process, thus it won't have anstdout
of its own.
– ZorgoZ
Jan 2 at 13:22
@Icepickle, I want to catch the Console.WriteLine output in form.cs
– Emily Wong
Jan 2 at 13:22
@ZorgoZ, yes I know that, I think you didn't get my question, what I want to do is to dynamically compile a piece of code in memory and gets its output in a variable without building any file. more specific, lets assume the code that I want to compile is a standalone command line application which have main() function, and writing the output into console. but i want to compile it in memory and get what it would write in console in a variable.
– Emily Wong
Jan 2 at 13:25
Would setting the out help you?
– Icepickle
Jan 2 at 13:26
|
show 4 more comments
1
You want to get the IL output? Or you want to catch theConsole.WriteLine
from your application that you would be calling?
– Icepickle
Jan 2 at 13:19
1
I might be wrong, but it looks you have confusion. The generated code is not a stand-alone process, it will be running as part of your currently running process, thus it won't have anstdout
of its own.
– ZorgoZ
Jan 2 at 13:22
@Icepickle, I want to catch the Console.WriteLine output in form.cs
– Emily Wong
Jan 2 at 13:22
@ZorgoZ, yes I know that, I think you didn't get my question, what I want to do is to dynamically compile a piece of code in memory and gets its output in a variable without building any file. more specific, lets assume the code that I want to compile is a standalone command line application which have main() function, and writing the output into console. but i want to compile it in memory and get what it would write in console in a variable.
– Emily Wong
Jan 2 at 13:25
Would setting the out help you?
– Icepickle
Jan 2 at 13:26
1
1
You want to get the IL output? Or you want to catch the
Console.WriteLine
from your application that you would be calling?– Icepickle
Jan 2 at 13:19
You want to get the IL output? Or you want to catch the
Console.WriteLine
from your application that you would be calling?– Icepickle
Jan 2 at 13:19
1
1
I might be wrong, but it looks you have confusion. The generated code is not a stand-alone process, it will be running as part of your currently running process, thus it won't have an
stdout
of its own.– ZorgoZ
Jan 2 at 13:22
I might be wrong, but it looks you have confusion. The generated code is not a stand-alone process, it will be running as part of your currently running process, thus it won't have an
stdout
of its own.– ZorgoZ
Jan 2 at 13:22
@Icepickle, I want to catch the Console.WriteLine output in form.cs
– Emily Wong
Jan 2 at 13:22
@Icepickle, I want to catch the Console.WriteLine output in form.cs
– Emily Wong
Jan 2 at 13:22
@ZorgoZ, yes I know that, I think you didn't get my question, what I want to do is to dynamically compile a piece of code in memory and gets its output in a variable without building any file. more specific, lets assume the code that I want to compile is a standalone command line application which have main() function, and writing the output into console. but i want to compile it in memory and get what it would write in console in a variable.
– Emily Wong
Jan 2 at 13:25
@ZorgoZ, yes I know that, I think you didn't get my question, what I want to do is to dynamically compile a piece of code in memory and gets its output in a variable without building any file. more specific, lets assume the code that I want to compile is a standalone command line application which have main() function, and writing the output into console. but i want to compile it in memory and get what it would write in console in a variable.
– Emily Wong
Jan 2 at 13:25
Would setting the out help you?
– Icepickle
Jan 2 at 13:26
Would setting the out help you?
– Icepickle
Jan 2 at 13:26
|
show 4 more comments
1 Answer
1
active
oldest
votes
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()}");
Console.WriteLine(sb.ToString()); does not return anything. Am I doing it correctly?
– Emily Wong
Jan 2 at 13:53
@EmilyWong. No, remember, SetOut is called before, the output redirection is valid. If you want the output back, saveConsole.Out
in a variable before callingSetOut
, then use it to restore the original output before trying to use it.
– ZorgoZ
Jan 2 at 13:55
@EmilyWong see update
– ZorgoZ
Jan 2 at 13:58
That works very well. made my day, Thanks!
– Emily Wong
Jan 2 at 14:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007097%2fc-sharp-dynamic-compiler-get-standard-output-when-compile-in-memory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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()}");
Console.WriteLine(sb.ToString()); does not return anything. Am I doing it correctly?
– Emily Wong
Jan 2 at 13:53
@EmilyWong. No, remember, SetOut is called before, the output redirection is valid. If you want the output back, saveConsole.Out
in a variable before callingSetOut
, then use it to restore the original output before trying to use it.
– ZorgoZ
Jan 2 at 13:55
@EmilyWong see update
– ZorgoZ
Jan 2 at 13:58
That works very well. made my day, Thanks!
– Emily Wong
Jan 2 at 14:09
add a comment |
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()}");
Console.WriteLine(sb.ToString()); does not return anything. Am I doing it correctly?
– Emily Wong
Jan 2 at 13:53
@EmilyWong. No, remember, SetOut is called before, the output redirection is valid. If you want the output back, saveConsole.Out
in a variable before callingSetOut
, then use it to restore the original output before trying to use it.
– ZorgoZ
Jan 2 at 13:55
@EmilyWong see update
– ZorgoZ
Jan 2 at 13:58
That works very well. made my day, Thanks!
– Emily Wong
Jan 2 at 14:09
add a comment |
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()}");
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()}");
edited Jan 2 at 14:14
answered Jan 2 at 13:43


ZorgoZZorgoZ
1,3111517
1,3111517
Console.WriteLine(sb.ToString()); does not return anything. Am I doing it correctly?
– Emily Wong
Jan 2 at 13:53
@EmilyWong. No, remember, SetOut is called before, the output redirection is valid. If you want the output back, saveConsole.Out
in a variable before callingSetOut
, then use it to restore the original output before trying to use it.
– ZorgoZ
Jan 2 at 13:55
@EmilyWong see update
– ZorgoZ
Jan 2 at 13:58
That works very well. made my day, Thanks!
– Emily Wong
Jan 2 at 14:09
add a comment |
Console.WriteLine(sb.ToString()); does not return anything. Am I doing it correctly?
– Emily Wong
Jan 2 at 13:53
@EmilyWong. No, remember, SetOut is called before, the output redirection is valid. If you want the output back, saveConsole.Out
in a variable before callingSetOut
, then use it to restore the original output before trying to use it.
– ZorgoZ
Jan 2 at 13:55
@EmilyWong see update
– ZorgoZ
Jan 2 at 13:58
That works very well. made my day, Thanks!
– Emily Wong
Jan 2 at 14:09
Console.WriteLine(sb.ToString()); does not return anything. Am I doing it correctly?
– Emily Wong
Jan 2 at 13:53
Console.WriteLine(sb.ToString()); does not return anything. Am I doing it correctly?
– Emily Wong
Jan 2 at 13:53
@EmilyWong. No, remember, SetOut is called before, the output redirection is valid. If you want the output back, save
Console.Out
in a variable before calling SetOut
, then use it to restore the original output before trying to use it.– ZorgoZ
Jan 2 at 13:55
@EmilyWong. No, remember, SetOut is called before, the output redirection is valid. If you want the output back, save
Console.Out
in a variable before calling SetOut
, then use it to restore the original output before trying to use it.– ZorgoZ
Jan 2 at 13:55
@EmilyWong see update
– ZorgoZ
Jan 2 at 13:58
@EmilyWong see update
– ZorgoZ
Jan 2 at 13:58
That works very well. made my day, Thanks!
– Emily Wong
Jan 2 at 14:09
That works very well. made my day, Thanks!
– Emily Wong
Jan 2 at 14:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007097%2fc-sharp-dynamic-compiler-get-standard-output-when-compile-in-memory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You want to get the IL output? Or you want to catch the
Console.WriteLine
from your application that you would be calling?– Icepickle
Jan 2 at 13:19
1
I might be wrong, but it looks you have confusion. The generated code is not a stand-alone process, it will be running as part of your currently running process, thus it won't have an
stdout
of its own.– ZorgoZ
Jan 2 at 13:22
@Icepickle, I want to catch the Console.WriteLine output in form.cs
– Emily Wong
Jan 2 at 13:22
@ZorgoZ, yes I know that, I think you didn't get my question, what I want to do is to dynamically compile a piece of code in memory and gets its output in a variable without building any file. more specific, lets assume the code that I want to compile is a standalone command line application which have main() function, and writing the output into console. but i want to compile it in memory and get what it would write in console in a variable.
– Emily Wong
Jan 2 at 13:25
Would setting the out help you?
– Icepickle
Jan 2 at 13:26