CodeDom : 编译部分类 [英] CodeDom : compile partial class

查看:40
本文介绍了CodeDom : 编译部分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译文本文件中的代码,以更改 WinForms 应用程序主窗体上 TextBox 中的值.IE.将另一个带有方法的分部类添加到调用表单中.该表单有一个按钮 (button1) 和一个 TextBox (textBox1).

I'm attempting to compile code in a text file to change a value in a TextBox on the main form of a WinForms application. Ie. add another partial class with method to the calling form. The form has one button (button1) and one TextBox (textBox1).

文本文件中的代码为:

this.textBox1.Text = "Hello World!!";

this.textBox1.Text = "Hello World!!";

和代码:

namespace WinFormCodeCompile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Load code from file
            StreamReader sReader = new StreamReader(@"Code.txt");
            string input = sReader.ReadToEnd();
            sReader.Close();

            // Code literal
            string code =
                @"using System;
                  using System.Windows.Forms;

                  namespace WinFormCodeCompile
                  {
                      public partial class Form1 : Form
                      {

                           public void UpdateText()
                           {" + input + @"
                           }
                       }
                   }";

            // Compile code
            CSharpCodeProvider cProv = new CSharpCodeProvider();
            CompilerParameters cParams = new CompilerParameters();
            cParams.ReferencedAssemblies.Add("mscorlib.dll");
            cParams.ReferencedAssemblies.Add("System.dll");
            cParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            cParams.GenerateExecutable = false;
            cParams.GenerateInMemory = true;

            CompilerResults cResults = cProv.CompileAssemblyFromSource(cParams, code);

            // Check for errors
            if (cResults.Errors.Count != 0)
            {
                foreach (var er in cResults.Errors)
                {
                    MessageBox.Show(er.ToString());
                }
            }
            else
            {
                // Attempt to execute method.
                object obj = cResults.CompiledAssembly.CreateInstance("WinFormCodeCompile.Form1");
                Type t = obj.GetType();
                t.InvokeMember("UpdateText", BindingFlags.InvokeMethod, null, obj, null);
            }


        }
    }
}

当我编译代码时,CompilerResults 返回一个错误,指出 WinFormCodeCompile.Form1 不包含 textBox1 的定义.

When I compile the code, the CompilerResults returns an error that says WinFormCodeCompile.Form1 does not contain a definition for textBox1.

有没有办法为调用程序集动态创建另一个部分类文件并执行该代码?

Is there a way to dynamically create another partial class file to the calling assembly and execute that code?

我想我在这里遗漏了一些非常简单的东西.

I assume I'm missing something really simple here.

推荐答案

分部类不能跨越程序集 - 程序集是编译单元,分部类一旦编译就变成了一个单独的类(CLR 层面没有等效的概念).

Partial classes can't span assemblies - assembly is the unit of compilation, and partial classes become a single class once compiled (there's no equivalent concept on CLR level).

这篇关于CodeDom : 编译部分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆