我正在尝试打开动态编译的表单,但是没有任何想法如何做到这一点。我可以执行表格所具有的功能,但是如何显示表格我不知道。
我在Form1类中执行测试函数的示例(它只是打开带有单词文本的MessageBox)
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);
type.GetMethod("test").Invoke(compiledObject, new object[] { });
}
}
}
}
Form.txt来源
using System.Windows.Forms;
namespace myForm
{
public partial class Form1 : Form
{
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;
}
}
答案 0 :(得分:1)
您可以通过调用Show()或ShowDialog()来显示与正常编译相同的方式(取决于您是否希望将表单显示为模式对话框):
Col1 Col2 Col3
XXX bbb 1 8 1
YYY bbb 8 7 6
ZZZ bbb 2 2 4
由于 type.GetMethod("Show", new Type[0]).Invoke(compiledObject, new object[] { });
方法有多个重载,您需要指定所需版本的参数类型。