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!"");
                    }
                }
            }"
            };
        }
    }
}

ルーンタイム コードから変数int xxにアクセスできるかどうかを知りたいです (たとえば、「hello world」の後に xx = 2; 行を置きます。それは素晴らしいことです:)

4

1 に答える 1

3

はい、利用可能にすることができますが、次のことを行う必要があります。

  • 含まれているアセンブリへの参照を追加します(便宜上、xxこのアセンブリを呼び出しています-おそらく、実行中のコンソールexeです)StaticAssemblyStaticAssembly.exe
  • アクセスできるように型Programを作るStaticAssemblypublic
  • 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 に答える