如何从 PropertyInfo 集合中, 得到集合属性的值...

btbtd 2007-05-06 07:39:51
比如我想遍历 Request 的属性,,,

下面代码是做到了...
---/-------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ import namespace="System.Reflection" %>
<script runat="server">
void Page_Load(Object s, EventArgs e)
{
EnumProperties(Request);
} // end Page_Load

void EnumProperties(Object Obj)
{
PropertyInfo[] Props = Obj.GetType().GetProperties();
for(int i=0, j=Props.Length; i<j; i++)
{
Response.Write("<li/>Props["+i+"].Name: "+Props[i].Name);
}
}
</script>


但是我想得到每个属性的对应值, 我发傻了...该怎么弄...
比如我想得到 Request.Url 的值...

是从PropertyInfo 集合中去获取, 如果是单一遍历, 这地球人都会...

...全文
830 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
btbtd 2007-05-07
  • 打赏
  • 举报
回复
:() 楼上...
上面的代码不是已经弄出来了吗, 俺还看那干吗...

public static void EnumProperties(Object Obj)
{ // shawl.qiu code
string ObjectName = Obj.ToString();
HttpContext.Current.Response.Write
(
"<h2 style='text-align:center; color:red;'>"+ObjectName+"</h2>"
);
PropertyInfo[] Props = Obj.GetType().GetProperties();
for(int i=0, j=Props.Length; i<j; i++)
{
HttpContext.Current.Response.Write
(
"<li/><span style='color:blue;font-weight:bold'>"+ObjectName+"."+Props[i].Name+"</span>: "
);
if(Props[i].CanRead)
{
try
{
object o = Props[i].GetValue(Obj, null);
if (o!= null)
{
HttpContext.Current.Response.Write(o);
}
}
catch(Exception ex)
{
HttpContext.Current.Response.Write
(
"<span style='color:red;font-weight:bold;'>"+ObjectName+"."+Props[i].ToString()+"</span>"
);
HttpContext.Current.Response.Write("<li/> Error Message: ");
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.Write("<hr/>");
} // end try
} // end if
} // end for
} // void EnumProperties

public static void EnumProperties()
{
EnumProperties("ServerVariables");
}

public static void EnumProperties(string cdti)
{
switch(cdti)
{
default:
int loop1, loop2;
NameValueCollection coll;
coll=HttpContext.Current.Request.ServerVariables;
String[] arr1 = coll.AllKeys;
string ObjectName = HttpContext.Current.Request.ServerVariables.GetType().ToString();
HttpContext.Current.Response.Write
(
"<h2 style='color:red;text-align:center'>"+
ObjectName+
"</h2>"
);
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
HttpContext.Current.Response.Write
(
"<li/><span style='color:blue;font-weight:bold;'>"+
"HttpContext.Current.Request.ServerVariables[\""+arr1[loop1] + "\"]</span>: "
);
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
HttpContext.Current.Response.Write(arr2[loop2] );
} // end for 1
} // end for
break;
} // end switch
} // end EnumProperties
Ivony 2007-05-06
  • 打赏
  • 举报
回复
请参阅:PropertyInfo.GetValue 方法

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref10/html/O_T_System_Reflection_PropertyInfo_GetValue.htm
btbtd 2007-05-06
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ import namespace="System.Reflection" %>
<script runat="server">
void Page_Load(Object s, EventArgs e)
{
EnumProperties(Request);
} // end Page_Load

void EnumProperties(Object Obj)
{
PropertyInfo[] Props = Obj.GetType().GetProperties();
for(int i=0, j=Props.Length; i<j; i++)
{
if(Props[i].CanRead)
{
try
{
object o = Props[i].GetValue(Request, null);
if (o!= null)
{
Response.Write("<li/>"+Props[i].Name+": "+o);
}
}
catch(Exception ex)
{
}
}
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- DW6 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template</title>
</head>
<body>
<form runat="server">

</form>
</body>
</html>
btbtd 2007-05-06
  • 打赏
  • 举报
回复
或者有什么方法可以适合遍历 NN 对象, 列出其属性和对应值...

方法不需要声明特定类型,,,
btbtd 2007-05-06
  • 打赏
  • 举报
回复
嫌分不够的可以再加,,,满意加到二百....

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧