1

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;


namespace ConsoleApplication1
{
    class Program
    {
        public int xx = 1; // variable I want to access from the runtime code

        static void Main(string[] args)
        {            
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;
            parameters.ReferencedAssemblies.Add("System.dll");


            CompilerResults results = provider.CompileAssemblyFromSource(parameters, GetCode());
            var cls = results.CompiledAssembly.GetType("ConsoleApplication1.Program");
            var method = cls.GetMethod("DynamicMethod", BindingFlags.Static | BindingFlags.Public);
            method.Invoke(null, null);

            int a = int.Parse(Console.ReadLine()); // pause console
        }       


        static string[] GetCode()
        {
            return new string[]
            {
            @"using System;

            namespace ConsoleApplication1
            {
                class Program
                {
                    public static void DynamicMethod()
                    {                        
                        Console.WriteLine(""Hello, world!"");
                    }
                }
            }"
            };
        }
    }
}

我想知道是否可以从 runetime 代码中访问变量int xx (例如,将 xx = 2; 放在“hello world”之后。那太棒了谢谢 :)

4

1 回答 1

3

是的,您可以提供它,但您需要:

  • 添加对包含的程序集的引用xx(为方便起见,我将其称为此程序集StaticAssembly- 所以它可能StaticAssembly.exe是正在运行的控制台 exe)
  • Program输入StaticAssembly一个类型,以便public它可以访问
  • 要么做成xx一个static字段,要么实例化一个实例Program并以某种方式将其传递给动态代码

例如,以下工作(我正在使用“实例化实例并传递它”方法,所以我添加了一个参数DynamicMethod):

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;


namespace StaticAssembly
{
    public class Program
    {
        public int xx = 1; // variable I want to access from the runtime code

        static void Main(string[] args)
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;
            parameters.ReferencedAssemblies.Add("System.dll");
            parameters.ReferencedAssemblies.Add("StaticAssembly.exe");

            CompilerResults results = provider.CompileAssemblyFromSource(parameters, GetCode());
            var cls = results.CompiledAssembly.GetType("GeneratedAssembly.Program");
            var method = cls.GetMethod("DynamicMethod", BindingFlags.Static | BindingFlags.Public);
            var p = new Program();
            p.xx = 42;
            method.Invoke(null, new object[] {p});

            int a = int.Parse(Console.ReadLine()); // pause console
        }


        static string[] GetCode()
        {
            return new string[]
            {
            @"using System;

            namespace GeneratedAssembly
            {
                class Program
                {
                    public static void DynamicMethod(StaticAssembly.Program p)
                    {                        
                        Console.WriteLine(p.xx);
                        Console.WriteLine(""Hello, world!"");
                    }
                }
            }"
            };
        }
    }
}
于 2012-08-29T09:53:28.450 回答