Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取动态编译C#代码的变量值_C#_Variables_Compiler Construction - Fatal编程技术网

如何获取动态编译C#代码的变量值

如何获取动态编译C#代码的变量值,c#,variables,compiler-construction,C#,Variables,Compiler Construction,我擅长在运行时构造一段代码并在运行时编译它。现在我需要在运行时在动态代码中获得一个变量的结果。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using Microsoft.CSharp; using System.CodeDom.Compiler; class Program { static void Main(string[] args) { var cs

我擅长在运行时构造一段代码并在运行时编译它。现在我需要在运行时在动态代码中获得一个变量的结果。这是我的密码:

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));
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.CSharp;
使用System.CodeDom.Compiler;
班级计划
{
静态void Main(字符串[]参数)
{
var csc=new CSharpCodeProvider(new Dictionary(){{{“compilervision”,“v3.5”});
var参数=新的编译器参数(新[]{“mscorlib.dll”,“System.Core.dll”},“Example.exe”,true);
parameters.GenerateExecutable=true;
CompilerResults results=csc.compileasemblyFromSource(参数,
@“使用System.Linq;
班级计划{
公共静态void Main(字符串[]args){
int abc=10;
bool myresult=false;
如果(abc==10)
myresult=true;
其他的
myresult=false;
}
}");
results.Errors.Cast().ToList().ForEach(error=>Console.WriteLine(error.ErrorText));
}
}

我希望运行时代码在我的主程序中返回myresultvaraible的值,以及如何获取该值。

您需要添加一个新的类似代码,以使用“Example.exe”作为引用,并使用“program”类来获得结果。此外,您还需要手动触发main,并且应该有一个静态变量来存储结果。在读取该变量之前,还需要保持该程序的完成

如果我理解正确,您正在编译并运行一个尚未创建的可执行文件。 这意味着,您需要假设它已成功创建,并以字符串形式使用代码,然后再创建一个可执行文件来运行它。

(1)您可以将已编译程序集作为一段代码加载,并使用直接传递协定的任何参数调用任何方法。下面是修改后的示例打印

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());
    }
}
使用Microsoft.CSharp;
使用制度;
使用System.CodeDom.Compiler;
使用System.Collections.Generic;
使用System.Linq;
运用系统反思;
班级计划
{
静态void Main(字符串[]参数)
{
var csc=new CSharpCodeProvider(new Dictionary(){{{“compilervision”,“v3.5”});
var参数=新的编译器参数(新[]{“mscorlib.dll”,“System.Core.dll”},“Example.exe”,true);
parameters.GenerateExecutable=true;
CompilerResults results=csc.compileasemblyFromSource(参数,
@“使用System.Linq;
班级计划{
公共静态void Main(字符串[]args){}
公共静态字符串Main1(int abc){
bool myresult=false;
如果(abc==10)
myresult=true;
其他的
myresult=false;
返回abc.ToString()+“”:“”+(myresult?”“myresult为true”:“myresult为false”);
}
}");
results.Errors.Cast().ToList().ForEach(error=>Console.WriteLine(error.ErrorText));
var scriptClass=results.CompiledAssembly.GetType(“程序”);
var scriptMethod1=scriptClass.GetMethod(“Main1”,BindingFlags.Static | BindingFlags.Public);
WriteLine(scriptMethod1.Invoke(null,新对象[]{10}).ToString());
WriteLine(scriptMethod1.Invoke(null,新对象[]{20}).ToString());
}
}

(2)或者,您可以使用所需的任何命令行参数运行编译后的
Example.exe
,解析它们并以任何方式返回结果。控制台应用程序通常通过文件传递复杂的输入或输出结果,总体成功/失败通过文件进行指示。有相当多的可用选项,其中一些选项在堆栈溢出问题中概述了

编译代码后,您可以使用
进程
类执行结果文件,并从中捕获
ExitCode
。顺便说一句:然后可以优化代码:
Environment.Exit(abc==10)我按照链接中给出的示例进行了尝试。。但是它仍然不起作用@sia遵循
ShowExpressionResults
(在您的链接中)中的示例加载已编译的程序集,在其中查找类,在类中查找方法,然后运行该方法并打印该方法的输出将重试并更新结果。。