如何通过类名创建对象 知道类名后,通过反射用Type创建的对象都是object类型的,怎么才能真正得到这个类的实例呢,请教下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考一下:System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFile((string)tsi.Tag); Type type = null; Type[] typeList = ass.GetTypes(); foreach (Type t in typeList) { if (t.IsClass && t.GetInterface(typeof(NoteNet.PlugIn.Interface.INoteNetPlugIn).FullName, true) != null) { type = t; break; } } if (type != null) { NoteNet.PlugIn.Interface.INoteNetPlugIn pobj = (NoteNet.PlugIn.Interface.INoteNetPlugIn)ass.CreateInstance(type.FullName, true); pobj.ShowPlugInOperateForm(this, new NoteNet.PlugIn.Interface.Module.NoteDataInfoModule()); } object is 你的对象 as 才对强制转换了,结果是null,这是怎么个情况 Activator.CreateInstance(typeof(...)); IObject obj1=(IObject)Activator.CreateInstance(System.Type.GetType ("ActivatorCreateInstance.ClassExam")); private void button1_Click(object sender, EventArgs e) { Type type = typeof(Form2); Form2 obj2 = (Form2)Activator.CreateInstance(type, new string[] {}); } (接口)Assembly.Load(路径).CreateInstance(类名)试下 创建不成功或转换为不兼容的类型了,如果不在同一程序集中,需要先Load程序集,然后再创建,看11楼 VS2008 C#打开异常 【求教】关于C#创建Windows服务程序!!!!急!!!! 跪求 C#重写控件 方面的书 C#调用API问题。 一个在comboBox控件中遇到的难题 动态引用DLL创建的对象怎么为属性赋值? 新手请教 如和将一个2维数组读取出来 做个循环 十万火急,急求救助 listview控件中的Items集合的项移动使用难题,着急! 嵌套类问题 关于网络编程UDP的异步方法(请高手指点!) 请教hook api的问题
System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFile((string)tsi.Tag);
Type type = null;
Type[] typeList = ass.GetTypes();
foreach (Type t in typeList)
{
if (t.IsClass && t.GetInterface(typeof(NoteNet.PlugIn.Interface.INoteNetPlugIn).FullName, true) != null)
{
type = t;
break;
}
} if (type != null)
{
NoteNet.PlugIn.Interface.INoteNetPlugIn pobj = (NoteNet.PlugIn.Interface.INoteNetPlugIn)ass.CreateInstance(type.FullName, true);
pobj.ShowPlugInOperateForm(this, new NoteNet.PlugIn.Interface.Module.NoteDataInfoModule());
}
强制转换了,结果是null,这是怎么个情况
{
Type type = typeof(Form2); Form2 obj2 = (Form2)Activator.CreateInstance(type, new string[] {});
}
试下