Activator.CreateInstance()的问题 [英] the problem of Activator.CreateInstance()

查看:72
本文介绍了Activator.CreateInstance()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Assembly a = Assembly.Load("Dll file");
 Type theType = a.GetType("ClassName");   
dynamic agDrvr = Activator.CreateInstance(ClassType);


这是使用反射创建某些Class的实例的常用功能

在上面的通用函数中,我只知道类名,而不是类类型,

因此,如何从
获取[Activator.CreateInstance(ClassType)]的结果
对象类型到类标记名称的类型是[className];


this is the common function to create the instance of some Class by use reflection

in the above of common function ,I just know the class name ,not the class type ,

so how can I make the result of the [Activator.CreateInstance(ClassType)] from the

type of Object to the type of class whitch name is [className];

推荐答案

您可以使用以下代码:
You could use this:
Activator.CreateInstance("Dll file", "ClassName");



您可以从Assembly对象获取程序集名称(上面的CreateInstance的firs参数)opr也可以使用该Assembly对象直接创建实例:



You can get the assembly name (firs parameter of CreateInstance above) from the Assembly object opr could also directly create an instance using that Assembly object:

Assembly a = Assembly.Load("Dll file");
MyObject o = a.CreateInstance("ClassName");


http://msdn.microsoft.com/en-us/library/dex1ss7c.aspx [ ^ ]

祝你好运!


http://msdn.microsoft.com/en-us/library/dex1ss7c.aspx[^]

Good luck!


这篇关于Activator.CreateInstance()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆