从动态编译的表单中获取数据

时间:2016-05-10 12:43:10

标签: c# dynamic compilation

我在动态编译的表单中有一个静态变量_testVar。我怎样才能从主程序中获得它的价值?我可以执行功能,但我不能返回值。我尝试使用ref,但它只是不起作用。

我在Form1类中执行测试函数的示例

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

namespace myConsoleCompiler
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var foo = new CSharpCodeProvider())
            {
                var parameters = new CompilerParameters
                {
                    GenerateInMemory = true,
                    GenerateExecutable = false
                };

                parameters.ReferencedAssemblies.Add("System.dll");
                parameters.ReferencedAssemblies.Add("System.Core.dll");
                parameters.ReferencedAssemblies.Add("System.Drawing.dll");
                parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");

                var source = File.ReadAllText("form.txt");
                CompilerResults results = foo.CompileAssemblyFromSource(parameters, source);
                Type type = results.CompiledAssembly.GetType("myForm.Form1");
                object compiledObject = Activator.CreateInstance(type);

                // Execure test function from dynamically compiled form
                type.GetMethod("test").Invoke(compiledObject, new object[] { });
            }
        }
    }
}

form.txt来源:

using System.Windows.Forms;

namespace myForm
{
    public partial class Form1 : Form
    {
        public static int _testVar = 0; // how can i get this var value?
        public Form1()
        {
            InitializeComponent();
        }

        public void test()
        {
            MessageBox.Show("test");
        }
    }
}

namespace myForm
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Location = new System.Drawing.Point(12, 12);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(260, 204);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 222);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(260, 40);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 274);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.richTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button button1;
    }
}

1 个答案:

答案 0 :(得分:1)

FieldInfo info = type.GetField("_testVar", BindingFlags.Public | BindingFlags.Static);
object value = info.GetValue(null);