积极答复者
泛型反射工厂如何使用,如何反射创建泛型子对象,如何在泛型工厂中使用反射

问题
-
为了让问题更加明白,我把项目的简化版创建了出来。
/*----------------3、两个类模型------------------*/
namespace W02Factory.MODELS
{
///<summary>
/// 学生表模型。为了明了,只定了两个属性
///</summary>
publicclassStudent
{
publicstring Name { get; set; }
publicstring Age { get; set; }
}
}
namespace W02Factory.MODELS
{
///<summary>
/// 学生成绩表。只定了两个属性
///</summary>
publicclassStudentScore
{
publicstring mathematics { get; set; }
publicstring chemistry { get; set; }
}
}
/*----------------4、泛型父类------------------*/
namespace W02Factory
{
///<summary>
/// 父类,定义共用的方法,重点:是泛型的。
/// 为了明了,假设只有Add和list方法
///</summary>
publicclassBaseTable<T> where T : class,new()
{
publicint Add()
{
return 1;
}
publicList<T> list(T model)
{
returnnull;
}
}
}
/*----------------5、两个泛型子类------------------*/
namespace W02Factory
{
///<summary>
/// 子类,操作Student
///</summary>
publicclassStudentBLL<T>:BaseTable<MODELS.Student> where T:class,new()
{
}
}
namespace W02Factory
{
///<summary>
/// 子类,操作StudentScore
///</summary>
publicclassStudentScoreBLL<T> : BaseTable<MODELS.StudentScore> where T : class,new()
{
}
}
/*----------------6、泛型反射工厂------------------*/
namespace W02Factory
{
///<summary>
/// 泛型反射工厂
///</summary>
publicclassReflectFactory<T> where T:class,new()
{
publicstaticBaseTable<T> CreateTable(string typeName)
{
----怎么写反射?是泛型的。----
//Assembly.LoadFile(@"C:\Users\Lin\Desktop\反射示例4\W01WebUI\bin\W02Factory.dll");
//Type type = Type.GetType(typeName, true);
//BaseTable<T> bt = Activator.CreateInstance(type) as BaseTable<T>;
//BaseTable<T> bt = Assembly.LoadFile(@"C:\Users\Lin\Desktop\反射示例4\W01WebUI\bin\W02Factory.dll").CreateInstance(typeName) as BaseTable<T>;
BaseTable<T> bt = Assembly.Load("W02Factory").CreateInstance(typeName) asBaseTable<T>;
return bt;
}
}
}
/*----------------7、控制器------------------*/
publicActionResultIndex()
{
BaseTable<Student>student =ReflectFactory<Student>.CreateTable("W02Factory.MODELS.Student");
ViewBag.student2 = student.Add();
returnView();
}
/*----------------8、视图------------------*/
@ViewBag.student2//我想把Add()返回的1显示出来,以验证成功与否。
/*----------------现在的问题是:------------------*/
在“6、泛型反射工厂”这部分,当如何使用反射?,然后在“7、控制器”中如何调用这个工厂?如果是普通的反射倒很容易,但是现在反射的是泛型的。 请大家指点一二,不胜感谢!
答案
-
Hi,在调用Assembly.CreateInstance或者Type.GetType时,如果要创建泛型,参数必须是“泛型类型名`1[类型名]",只要参数正确,都能返回正确的类型,比如,要使用Type.GetType来创建的话:
public static BaseTable<T> CreateTable(string typeName) { Type type = Type.GetType("W02Factory.BaseTable`1[" + typeName + "]"); BaseTable<T> bt = Activator.CreateInstance(type) as BaseTable<T>; return bt; }
在使用时:
BaseTable<Student>student = ReflectFactory<Student>.CreateTable("W02Factory.MODELS.Student");
- 已标记为答案 LookMVC 2014年4月1日 11:07
全部回复
-
-
Hi,在调用Assembly.CreateInstance或者Type.GetType时,如果要创建泛型,参数必须是“泛型类型名`1[类型名]",只要参数正确,都能返回正确的类型,比如,要使用Type.GetType来创建的话:
public static BaseTable<T> CreateTable(string typeName) { Type type = Type.GetType("W02Factory.BaseTable`1[" + typeName + "]"); BaseTable<T> bt = Activator.CreateInstance(type) as BaseTable<T>; return bt; }
在使用时:
BaseTable<Student>student = ReflectFactory<Student>.CreateTable("W02Factory.MODELS.Student");
- 已标记为答案 LookMVC 2014年4月1日 11:07