I can use CodeDomProvider for compiling the C# Code. I compiled and got the compiled result. But I want to get the output of the code snippet below. How do i get this?
我可以使用CodeDomProvider来编译C#代码。我编译并得到了编译结果。但我想得到下面代码片段的输出。我怎么得到这个?
e.g.:
for(int i=0;i<=5;i++)
{
System.Console.WriteLine(i);
}
The above code is what i am using. I can successfully compile it but did not get the result. How can I get the result?
上面的代码是我正在使用的。我可以成功编译它但没有得到结果。我怎样才能得到结果?
1 个解决方案
#1
0
Yes you can compile and get the results too. You need to have your code under namespace and within a function
是的,你可以编译并获得结果。您需要在命名空间和函数内使用代码
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("Dynamic.Test");
MethodInfo main = program.GetMethod("Test");
main.Invoke(null, null);
Above Summary from https://msdn.microsoft.com/en-us/library/saf5ce06(v=vs.100).aspx and http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime
以上摘要来自https://msdn.microsoft.com/en-us/library/saf5ce06(v=vs.100).aspx和http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-在运行时
EDIT-
Created below program with some modifications to read result. Hope this helps.
在程序下创建,对读取结果进行一些修改。希望这可以帮助。
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CSharp;
namespace ConsoleApplication1
{
internal class Program
{
static void Main(string[] args)
{
string code = @"
using System;
using System.Collections.Generic;
namespace Dynamic1
{
public class Test
{
public static IEnumerable<int> Test1()
{
for(int i=0;i<=5;i++)
{
yield return i;
}
}
}
}
";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters =new CompilerParameters();
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("Dynamic1.Test");
MethodInfo main = program.GetMethod("Test1");
IEnumerable<int> outputResults = (IEnumerable<int>)main.Invoke(null, null);
foreach (var result in outputResults)
{
Console.WriteLine(result);
}
Console.ReadLine();
}
}
#1
0
Yes you can compile and get the results too. You need to have your code under namespace and within a function
是的,你可以编译并获得结果。您需要在命名空间和函数内使用代码
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("Dynamic.Test");
MethodInfo main = program.GetMethod("Test");
main.Invoke(null, null);
Above Summary from https://msdn.microsoft.com/en-us/library/saf5ce06(v=vs.100).aspx and http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime
以上摘要来自https://msdn.microsoft.com/en-us/library/saf5ce06(v=vs.100).aspx和http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-在运行时
EDIT-
Created below program with some modifications to read result. Hope this helps.
在程序下创建,对读取结果进行一些修改。希望这可以帮助。
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CSharp;
namespace ConsoleApplication1
{
internal class Program
{
static void Main(string[] args)
{
string code = @"
using System;
using System.Collections.Generic;
namespace Dynamic1
{
public class Test
{
public static IEnumerable<int> Test1()
{
for(int i=0;i<=5;i++)
{
yield return i;
}
}
}
}
";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters =new CompilerParameters();
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("Dynamic1.Test");
MethodInfo main = program.GetMethod("Test1");
IEnumerable<int> outputResults = (IEnumerable<int>)main.Invoke(null, null);
foreach (var result in outputResults)
{
Console.WriteLine(result);
}
Console.ReadLine();
}
}