Ok, I have found a couple of solutions neither of which im entiirely happy with:
Solution 1:
CODE
public Control LoadControl(string typeName)
{
return LoadControl(Type.GetType(typeName));
}
public Control LoadControl(Type controlType)
{
if (controlType != null)
{
return Activator.CreateInstance(controlType) as Control;
}
return null;
}
private void button1_Click(object sender, EventArgs e)
{
Control test = LoadControl("System.Windows.Forms.TextBox, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
if (test != null)
{
this.Controls.Add(test);
}
}
The problem with the above is that I have to specify the fully qualified asembly name which i dont want to do for compatibility reasons (.NET 1 and 3 will have different public key tokens etc.)
Solution 2:
CODE
string strType = "System.Windows.Forms.TextBox";
Assembly asm = Assembly.LoadWithPartialName("System.Windows.Forms");
Type tp = asm.GetType(strType);
object obj = Activator.CreateInstance(tp);
Unfortunatly LoadWithPartialName is now obsolete in framework 2 (although you can still use it), it has been replaced with Assembly.Load()
Now unfortunatly Assembly.Load("System.Windows.Forms"), doesnt work.
I need to find a way of creating the System.Windows.Forms assembly without having to use the LoadFrom method with the Path as the parameter e.g.
Assembly.LoadFrom(@"C:\WINDOWS\Microsoft.NET\Framework\*version*\System.Windows.Forms.dll");
Any ideas???