我正在尝试通过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
{
}
我真的不确定还有什么东西......我对这些东西很新,我似乎无法理解我遇到的文章。
答案 0 :(得分:2)
如果您是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")));
}
答案 1 :(得分:1)
假设您现在实际上能够创建EXE(因为您在using
声明中缺少一些Form1
语句),我将首先添加入口点static {{ 1}}创建并显示新Main
实例的方法:
Form1
当您运行生成的EXE时,至少应该出现一个窗口。 @soandos也有一个好处,你应该能够从在Visual Studio中创建表单时创建的源中复制和粘贴,虽然你应该记住VS2008 +使用了部分类,所以你需要结合Form1.cs的内容和Form1.Designer.cs。