2018/07/11(Wed) 14:00:17 編集(投稿者)
リフレクションを使わない方法
public interface IFormMaker {
Form Make();
}
public class FormMaker<T> : IFormMaker where T : Form, new()
{
public Form Make()
{
return new T();
}
}
Dictionary<Button, IFormMaker> Btns = new Dictionary<Button, IFormMaker>();
Btns.Add(btn1, new FormMaker<Form1>());
Btns.Add(btn2, new FormMaker<Form2>());
private void button_Click(object sender, EventArgs e)
{
IFormMaker maker = Btns[(Button)sender];
Form frm = maker.Make();
frm.Show();
}
---
追記
あるいは、辞書化せずに
private void AddButton<T>(string name) where T : Form, new()
{
var button1 = new System.Windows.Forms.Button();
button1.Click += delegate {
Form form = new T();
form.Show();
};
}