Reputation: 1910
I'm at constructing a piece of code at run-time and compiling it run-time. Now i need to get the result of a variable in dynamic code at run-time. Here is my code :
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
class Program
{
static void Main(string[] args)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "Example.exe", true);
parameters.GenerateExecutable = true;
CompilerResults results = csc.CompileAssemblyFromSource(parameters,
@"using System.Linq;
class Program {
public static void Main(string[] args) {
int abc=10;
bool myresult=false;
if(abc==10)
myresult=true;
else
myresult=false;
}
}");
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
}
}
I want runtime code to return value of myresult varaible in my main program and how do i get it.
Upvotes: 2
Views: 1730
Reputation: 8145
(1) You can load the compiled assembly as piece of code and call any method with any argument passing contract directly. Following modified example prints
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "Example.exe", true);
parameters.GenerateExecutable = true;
CompilerResults results = csc.CompileAssemblyFromSource(parameters,
@"using System.Linq;
class Program {
public static void Main(string[] args) {}
public static string Main1(int abc) {
bool myresult=false;
if(abc==10)
myresult=true;
else
myresult=false;
return abc.ToString() + "": "" + (myresult ? ""myresult is true"" : ""myresult is false"");
}
}");
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
var scriptClass = results.CompiledAssembly.GetType("Program");
var scriptMethod1 = scriptClass.GetMethod("Main1", BindingFlags.Static | BindingFlags.Public);
Console.WriteLine(scriptMethod1.Invoke(null, new object[] { 10 }).ToString());
Console.WriteLine(scriptMethod1.Invoke(null, new object[] { 20 }).ToString());
}
}
(2) Or you can run the compiled Example.exe
with any command line arguments you want, parse them and return the results any way you want. Console applications usually communicate complex input or output results through files and overall success/failure indicate through Process.ExitCode. There are quite many available options, some of them outlined in Stack Overflow question Returning a string from a console application
Upvotes: 1
Reputation: 678
You need to add a new similar code to use "Example.exe" as a reference and use "Program" class to get the result. Also, you will need to manually trigger the main and should have a static vaariable to store the result. Also need to make hold the completion of that program before reading that variable.
If I am understanding it correct, you are compiling and runnign an Executable, which is yet to be created. That means, you need to assume its successful creation and work with your code in a string and have one more executable created to run it.
Upvotes: 1