110,538
社区成员
发帖
与我相关
我的任务
分享
// 方法1
// 这里UserClass 必须有一个无参构造函数
// 如果是有参数的需要使用构造起来构造这个对象
object o = Activator.CreateInstance(Type.GetType("Model1." + ClassName + ",test"));
// 获得所有属性,这里能获得Name和Age
PropertyInfo[] pis = o.GetType().GetProperties();
foreach(PropertyInfo info in pis){
switch(info.Name){
case "Name": {
info.SetValue(o, name, null);
break;
}
case "Age": {
info.SetValue(o, Age, null);
break;
}
}
}
Model1.UserClass user = o as Model1.UserClass;
// 方法2
Model1.UserClass u = Activator.CreateInstance(Type.GetType("Model1." + ClassName + ",test")) as Model1.UserClass;
if (u != null) {
u.Name = name;
u.Age = Age;
}
try
{
Form frmCur = null;
Assembly assDLL = Assembly.LoadFrom(Application.StartupPath + "\\" + "信息管理系统.exe");
if (assDLL == null)
MessageBox.Show("找不到文件:" + "TextForm" + "\n需要更新程序。");
frmCur = (Form)assDLL.CreateInstance("信息管理系统.TextForm");
if (frmCur == null)
MessageBox.Show("需要更新程序。");
frmCur.Text = "你好啊";
frmCur.Show();
}
catch
{
MessageBox.Show("加载程序集出错,请联系开发人员!");
}