Class reflection sample
Reflection.aspx

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Reflection.aspx.cs" Inherits="Reflection" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Class reflection sample</title>

<style>

div{
}{font-size:12px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>Class reflection sample</h3>
<div>
<asp:Label runat="server" ID="ProperList">
</asp:Label>
<hr />
<asp:Label runat="server" ID="MethodList">
</asp:Label>
<hr />
<asp:Label runat="server" ID="EventList">
</asp:Label>
<hr />
</div>
</form>
</body>
</html>

Reflection.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;

public partial class Reflection : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs ea)

{
//Type t = Type.GetType("System.Web.UI.WebControls.LinkButton");

Type t = typeof(LinkButton);


ConstructorInfo ci = t.GetConstructor(new Type[]
{ }); //获取当前 Type 的特定构造函数。
object o = ci.Invoke(null); //调用该实例反映的构造函数

{
PropertyInfo p = t.GetProperty("Text"); //获取当前 Type 的特定属性
p.SetValue(o, "Hello world", null);
}

PropertyInfo[] propers = t.GetProperties();//获取当前 Type 的属性。

foreach (PropertyInfo p in propers)

{
ProperList.Text += p.Name + " = " + p.GetValue(o, null) + "<br/>";
}

//方法
MethodInfo[] methods = t.GetMethods();

foreach (MethodInfo m in methods)

{
if (m.IsPublic == true)

{
MethodList.Text += " public ";
}

if (m.IsVirtual == true)

{
MethodList.Text += " virtual ";
}
if (m.IsStatic == true)

{
MethodList.Text += " static ";
}

MethodList.Text += "<span style=\"color:blue\">" + m.ReturnType.ToString() + "</span> " + m.Name + " (";
ParameterInfo[] paras = m.GetParameters();

bool IsParameter = false;

foreach (ParameterInfo pa in paras)

{
MethodList.Text += "<span style=\"color:blue\">" + pa.ParameterType.ToString() + "</span> " + pa.Name + ",";

IsParameter = true;
}

if (IsParameter == true)

{
MethodList.Text = MethodList.Text.Substring(0, MethodList.Text.Length - 1);
}

MethodList.Text += ")<br/>";
}

//事件
EventInfo[] events = t.GetEvents();

foreach (EventInfo e in events)

{
EventList.Text += e.Name;

EventList.Text += "<br/>";
}

this.form1.Controls.Add((Control)o);
}
}

结果

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Reflection.aspx.cs" Inherits="Reflection" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Class reflection sample</title>
<style>

div{
}{font-size:12px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>Class reflection sample</h3>
<div>
<asp:Label runat="server" ID="ProperList">
</asp:Label>
<hr />
<asp:Label runat="server" ID="MethodList">
</asp:Label>
<hr />
<asp:Label runat="server" ID="EventList">
</asp:Label>
<hr />
</div>
</form>
</body>
</html>
Reflection.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
public partial class Reflection : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs ea)
{
//Type t = Type.GetType("System.Web.UI.WebControls.LinkButton");
Type t = typeof(LinkButton);

ConstructorInfo ci = t.GetConstructor(new Type[]
{ }); //获取当前 Type 的特定构造函数。
object o = ci.Invoke(null); //调用该实例反映的构造函数

{
PropertyInfo p = t.GetProperty("Text"); //获取当前 Type 的特定属性
p.SetValue(o, "Hello world", null);
}
PropertyInfo[] propers = t.GetProperties();//获取当前 Type 的属性。
foreach (PropertyInfo p in propers)
{
ProperList.Text += p.Name + " = " + p.GetValue(o, null) + "<br/>";
}
//方法
MethodInfo[] methods = t.GetMethods();
foreach (MethodInfo m in methods)
{
if (m.IsPublic == true)
{
MethodList.Text += " public ";
}
if (m.IsVirtual == true)
{
MethodList.Text += " virtual ";
}
if (m.IsStatic == true)
{
MethodList.Text += " static ";
}
MethodList.Text += "<span style=\"color:blue\">" + m.ReturnType.ToString() + "</span> " + m.Name + " (";
ParameterInfo[] paras = m.GetParameters();
bool IsParameter = false;
foreach (ParameterInfo pa in paras)
{
MethodList.Text += "<span style=\"color:blue\">" + pa.ParameterType.ToString() + "</span> " + pa.Name + ",";
IsParameter = true;
}
if (IsParameter == true)
{
MethodList.Text = MethodList.Text.Substring(0, MethodList.Text.Length - 1);
}
MethodList.Text += ")<br/>";
}
//事件
EventInfo[] events = t.GetEvents();
foreach (EventInfo e in events)
{
EventList.Text += e.Name;
EventList.Text += "<br/>";
}
this.form1.Controls.Add((Control)o);
}
}
结果
