look into System.Activator class, it has a bunch of CreateInstance methodsif you need to load the assembly first, look int System.Reflection.Assembly class, it also has bunch of CreateInstance methods
调试欢乐多
Assembly formAsm=Assembly.LoadFrom("WindowsControlLibraryTest.dll");
Type formType=formAsm.GetType("WindowsControlLibraryTest.UserControl1");
object formObj=Activator.CreateInstance(formType);
Form theForm=(Form)formObj;
theForm.Show();
Form obj;
obj = (Form)System.Activator.CreateInstance(
Type.GetType("System.Windows.Forms.Form,System.Windows.Forms"));
obj.Name = "MYFORM";
obj.Show();运行出错:
值不能为空
参数:Type
应该为是你的类名
http://www.c-sharpcenter.com/CSNET/dynamicinvoke.asp
http://www.c-sharpcorner.com/Code/2002/April/LoadingAssemblyInfo.asp
static void Main(string[] args)
{
String sTypeName = "a.ClassA";
String sPath = "C:/"; //System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom("a.dll");
System.Reflection.Assembly asm = System.Reflection.Assembly.Load("a");
Object o = asm.CreateInstance(sTypeName, false, BindingFlags.CreateInstance, null, new Object[] {sPath}, System.Globalization.CultureInfo.CurrentCulture, null); Console.WriteLine(o.ToString()); Console.ReadLine();
}