Mentioning two options here:
Below option is on the same line on which you have mentioned. You need to pass the type as argument and then use CreateInstance() method of type. Also will have to cast the instance to specific control.
public Control CreateControlInstance(Type type, int x, int y, int w, int h)
{
Control c = (Control) Activator.CreateInstance(type);
c.Location = new Point(x, y);
c.Size = new Size(w, h);
return c;
}
private main()
{
TextBox t = (TextBox) CreateControlInstance(typeof (TextBox), 1, 1, 100, 100);
}
Also you can write the generic function like mentioned below:
public static T CreateControlInstance<T>(int x, int y, int w, int h) where T : Control, new()
{
T c = new T();
c.Location = new Point(x, y);
c.Size = new Size(w, h);
return c;
}
private main()
{
TextBox t = CreateControlInstance<TextBox>(1, 1, 100, 100);
}