|
发表于 2020-2-23 23:00:01
|
显示全部楼层
利用反射,给一段例子给你.
string sClass = CCConvert.GetRequsetQueryString("ClassName");
string sMethodName = "GetReports";
//得到类
Type oType = System.Type.GetType("Report.Class."+sClass);
if(oType == null )
{
Response.Write("类:"+sClass +"不存在!");
return;
}
//实现化
object oInst = Activator.CreateInstance(oType);
if(oInst ==null)
{
Response.Write("类:"+sClass +"不存在!");
return;
}
//得到方法
MethodInfo oMethod = oType.GetMethod(sMethodName);
if(oMethod ==null)
{
Response.Write("方法:"+ sMethodName +"不存在!");
return ;
}
int iParamsCount = oMethod.GetParameters().Length;
string sParams = CCConvert.GetRequsetQueryString("Params");
string[] ary = sParams.Split(',');
if(ary.Length !=iParamsCount)
{
Response.Write(String.Format("传入的参数的个数{0}不等于该方法{1}实际的参数个数{2},请检查",ary.Length.ToString(),oMethod.Name,iParamsCount.ToString()));
return;
}
//得到属性
PropertyInfo Pro = oType.GetProperty("Title");
this.sTitle = Pro.GetValue(oInst,null).ToString();
FieldInfo fldFileName = oType.GetField("sExcelFileName");
string sHTML = oMethod.Invoke(oInst,BindingFlags.Public,Type.DefaultBinder,ary,null).ToString();
string sFileName = fldFileName.GetValue(oInst).ToString();
//Response.Write(sFileName);
|
|