怎样用反射根据类型名称生成一个实例?
namespace Bizs
{
class RoleBiz
{
}
}
这个类在另外一个DLL中编译
在ASPX界面层的某个用户控件*.ascx.cs中用下面的代码,想生成一个RoleBiz的实例为null。怎样才行?
string bizTypeName = "Bizs.RoleBiz";
//我试过GetEntryAssembly(),GetExecuteAssembly()都 不行啊。
object biz = Assembly.GetCallingAssembly().CreateInstance(bizTypeName, true);
Response.Write((biz == null).ToString()); // 这里看到是true;即biz是null
Response.Write(typeof(Bizs.RoleBiz).FullName);
return;
MethodInfo method = biz.GetType().GetMethod("SelectByPrimaryKeyValue");
object entity= method.Invoke(biz, new object[] { 73 });
有没有没有只根据类型名称生成一个实例啊?谢谢。
问题点数:10、回复次数:2Top
1 楼chjlcn(http://www.chenjiliang.com)回复于 2006-03-08 17:58:05 得分 0
是VS2005的开发环境,RoleBiz也是有公共无参构造函数的。在这里简略了。
还有就是用VS2005开发时,如果在线,老是重启。不上网就没事,是不是跟盗版有关啊。Top
2 楼yxrj()回复于 2006-03-08 20:58:32 得分 10
必须知道class RoleBizs所在的程序集
假设为Bizs.dll
object biz = Assembly.GetCallingAssembly().CreateInstance(bizTypeName, true);
改为
object biz = Assembly.Load("Bizs").CreateInstance(bizTypeName, true);
Top




