///利用反射实现动态实例化类,并且调用类的方法和参数
代码
/// <summary>
/// 通过函数取出指标数据
///利用反射实现动态实例化类,并且调用类的方法和参数
/// </summary>
/// <returns></returns>
public DataTable FunctionCalc(string dateflag, string cycle, string deptids)
{
model.IsStop = "0";
DataTable dtitem = dalitem.GetList(model).Tables[0];
DataRow[] dritemid = dtitem.Select("itemfrom ='" + HCMS.Common.Constant.ITEMFROM.FUNCTION + "' and formula is not null");
string strformula = "";
string stritemid = "";
DataTable dt = new DataTable();
foreach (DataRow item in dritemid)
{
stritemid = item["itemid"].ToString();
strformula = item["formula"].ToString(); //得到数据库存的类名
object obj;
Type t;
t = Type.GetType("MSBLL." + strformula); //得到这个类
MethodInfo methodInfo = t.GetMethod("CalcWorkLoad"); //得到方法
ParameterInfo[] paramsInfo = methodInfo.GetParameters();//得到指定方法的参数列表
int length = paramsInfo.Length; //参数的个数
object[] count = new object[length]; //定义OBJECT类型的数组
//给每个参数赋值
for (int i = 0; i < length; i++)
{
count[i] = "ss";
}
object instance = Activator.CreateInstance(t); //把t实例化了
obj = methodInfo.Invoke(instance, count); //参数必须是OBJECT和OBJECT[]类型
dt = (DataTable)obj;
//第二种方法:先反射后接口
//stritemid = item["itemid"].ToString();
//strformula = item["formula"].ToString();
//object obj;
//Type t;
//t = Type.GetType("MSBLL." + strformula);
//obj = Activator.CreateInstance(t);
//MSBLL.CalFunction.IFunction iclass = (MSBLL.CalFunction.IFunction)obj;
////dt = iclass.CalcWorkLoad(stritemid,dateflag, cycle, deptids);
}
return dt;
}
/// 通过函数取出指标数据
///利用反射实现动态实例化类,并且调用类的方法和参数
/// </summary>
/// <returns></returns>
public DataTable FunctionCalc(string dateflag, string cycle, string deptids)
{
model.IsStop = "0";
DataTable dtitem = dalitem.GetList(model).Tables[0];
DataRow[] dritemid = dtitem.Select("itemfrom ='" + HCMS.Common.Constant.ITEMFROM.FUNCTION + "' and formula is not null");
string strformula = "";
string stritemid = "";
DataTable dt = new DataTable();
foreach (DataRow item in dritemid)
{
stritemid = item["itemid"].ToString();
strformula = item["formula"].ToString(); //得到数据库存的类名
object obj;
Type t;
t = Type.GetType("MSBLL." + strformula); //得到这个类
MethodInfo methodInfo = t.GetMethod("CalcWorkLoad"); //得到方法
ParameterInfo[] paramsInfo = methodInfo.GetParameters();//得到指定方法的参数列表
int length = paramsInfo.Length; //参数的个数
object[] count = new object[length]; //定义OBJECT类型的数组
//给每个参数赋值
for (int i = 0; i < length; i++)
{
count[i] = "ss";
}
object instance = Activator.CreateInstance(t); //把t实例化了
obj = methodInfo.Invoke(instance, count); //参数必须是OBJECT和OBJECT[]类型
dt = (DataTable)obj;
//第二种方法:先反射后接口
//stritemid = item["itemid"].ToString();
//strformula = item["formula"].ToString();
//object obj;
//Type t;
//t = Type.GetType("MSBLL." + strformula);
//obj = Activator.CreateInstance(t);
//MSBLL.CalFunction.IFunction iclass = (MSBLL.CalFunction.IFunction)obj;
////dt = iclass.CalcWorkLoad(stritemid,dateflag, cycle, deptids);
}
return dt;
}