使用Codedom C#的Windows窗体应用程序

| 我正在尝试通过Codedom生成Windows应用程序表单。我找到了一个很好的示例,向我展示了如何为控制台应用程序执行此操作,但似乎无法在Windows窗体上实现此功能。 这是我到目前为止的内容:
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider(\"CSharp\");
            string Output = \"Out.exe\";
            Button ButtonObject = (Button)sender;

            textBox2.Text = \"\";
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            //Make sure we generate an EXE, not a DLL
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly = Output;
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);
Textbox1.text包含以下内容:
Public Class Form1: Form
{

}
我真的不确定还有什么...我对这些东西很陌生,我似乎无法理解我所遇到的文章。     
已邀请:
如果您是CodeDom的新手,我强烈建议您使用Linq2CodeDom,它允许您在表达式中编写代码,以后再通过CodeDom将其转换为VB或C#代码。使用此库,您可以编写如下内容:
public void Generate() 
{
    var c = new CodeDomGenerator();
    c.AddNamespace(\"Samples\")
     .AddClass(\"Form1\")
     .AddMethod(MemberAttributes.Public | MemberAttributes.Static, ()=>\"YourMethodName\", Emit.stmt(() => MessageBox.Show(\"Method Body\")));
}
    
假设您目前确实能够创建EXE(因为您在
Form1
声明中缺少一些
using
语句),我将首先添加入口点,即创建并显示新的
Form1
实例的静态
Main
方法:
using System;
using System.Windows.Forms;

public class Form1 : Form
{
    public static void Main(string[] args)
    {
        var form1 = new Form1();
        Application.Run(form1);
    }
}
运行生成的EXE时,至少应该会出现一个窗口。 @soandos也很不错,您应该能够从在Visual Studio中创建表单时创建的源中复制和粘贴,尽管您应该记住VS2008 +使用局部类,所以您需要将Form1.cs和Form1.Designer.cs。     

要回复问题请先登录注册