Simplest example of ConstructorInfo.Invoke
namespace TesterApp
{
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Assembly ass = Assembly.GetExecutingAssembly();
Type typa = ass.GetType(“TesterApp.Abc”);
{
Assembly ass = Assembly.GetExecutingAssembly();
Type typa = ass.GetType(“TesterApp.Abc”);
Type[] types ={typeof(int)};
ConstructorInfo csInfo =typa.GetConstructor(types);
object [] obj = { 10 };
object [] obj = { 10 };
var AbcObj = csInfo.Invoke(obj) as Abc;
AbcObj.Show();
}
}
public class Abc
{
private int _a;
public Abc(int a)
{
_a = a;
}
public void Show()
{
MessageBox.Show(_a.ToString());
}
}
}
}
Comments