c# - winform 的动态代码编译在 C# 中给出错误

标签 c# .net winforms dynamic-compilation

我有一个简单的 Windows 应用程序,其中创建了一个动态的 Win Form 并与工具箱一起显示。 用户将控件拖放到这个动态创建的表单上,并相应地编写代码。 下面不是完整的代码,而是我面临问题的部分。我正在尝试编译用户在运行时编写的代码,但它给我错误“在表单 0 -> 当前上下文行 (12) 错误中不存在名称‘InitializeComponent’:CS0103”

            // small piece of code
            string SecondLine = @"public partial class Form1 : Form
                                   {
                                      public Form1()
                                      {
                                         InitializeComponent();
                                      }
                                   }";


            Form1 frm =new Form1();

        frm.textBox1.Text = "using System;" + Environment.NewLine
        + "using System.IO;" + Environment.NewLine + "using System.Drawing;" +                    Environment.NewLine + "using System.Windows.Forms;" + Environment.NewLine + Environment.NewLine + "namespace MiniCompiler{" + Environment.NewLine + Environment.NewLine;

            frm.textBox1.Text = frm.textBox1.Text + SecondLine.ToString();
            frm.textBox1.Text = frm.textBox1.Text + Environment.NewLine + Environment.NewLine + "static class Program{" + Environment.NewLine + " [STAThread] " + Environment.NewLine + "static void Main()" + "{" + Environment.NewLine;


            string firstLine = "Application.EnableVisualStyles(); " + Environment.NewLine + "Application.SetCompatibleTextRenderingDefault(false);" + Environment.NewLine + "Application.Run(new Form1());";

            frm.textBox1.Text = frm.textBox1.Text + Environment.NewLine + firstLine;   
            frm.textBox1.Text = frm.textBox1.Text.ToString() + Environment.NewLine + "}" ;

//编译代码

 CSharpCodeProvider provider = new CSharpCodeProvider();
 ICodeCompiler compiler = provider.CreateCompiler();
 CompilerResults result = compiler.CompileAssemblyFromSource(param,code); 

我真的不确定在这里编译 Winform 有什么问题。

谢谢,

最佳答案

我认为错误消息是正确的。我看不到在 Form1 类的构造函数中调用的 InitializeComponent()-方法的定义位置。

因为 Form 是作为部分类生成的,所以可能有(实际上默认情况下有)多个文件,其中包含该类的成员。默认情况下你有两个文件。在你的例子中是 Form1.cs 和 Form1.Designer.cs。两者共同描述了 Form1 类。

方法 InitializeComponent 不是继承的。它在同一个类中定义,只是在另一个文件中。

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Text = "Form1";
    }

您可以将此方法从 Form1-Class 的其他部分复制到您的 SecondLine-String 中。然后它应该工作,我想。

Look in this file

关于c# - winform 的动态代码编译在 C# 中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26035131/

相关文章:

.net - 寻找基于 .NET (SQL-Server) 的测试管理工具

c# - 覆盖 WinForm ListView 控件上的 Drawitem 事件

.net - 我应该如何在这段 winform 代码中使用 IoC?

c# - 获取未设置为对象实例的对象引用的空引用对象的类型

c# - VS 2015 已导入具有相同标识的多个程序集

c# - ProgressBar 十进制值

c# - 具有现有数据库 View 的 Entity Framework 代码优先

c# - ODAC 11.2 版本 4 (11.2.0.3.0) 抛出 "Oracle 11.2.0.2.0 does not support APPLY"异常

c# - 如何在 AppenderSkeleton log4net 中添加自定义属性

c# - HttpClient 不在 CookieContainer 中存储 cookie