超难问题(对我),高手请进
怎么把一个实体类中所有的属性读出来,而且把属性的值也读出来?
------解决方案--------------------用反射吧
------解决方案--------------------利用反射,请参考:
string sClass = CCConvert.GetRequsetQueryString( "ClassName ");
string sMethodName = "GetReports ";
//得到类
Type oType = System.Type.GetType( "CallCenter.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);
------解决方案--------------------UP
------解决方案--------------------反射
------解决方案--------------------Class1 class1 = new Class1();
PropertyInfo[] arrayinfo = class1.GetType().GetProperties();
foreach (PropertyInfo info in arrayinfo)
{
object value = info.GetValue(class1, null);
}
------解决方案--------------------mark