Class reflection sample
Reflection.aspx
![]() <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reflection.aspx.cs" Inherits="Reflection" %>
<%@ 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">
<!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" >
<html xmlns="http://www.w3.org/1999/xhtml" >
![]() <head runat="server">
<head runat="server">
![]() <title>Class reflection sample</title>
    <title>Class reflection sample</title>
![]() <style>
    <style>
![]() div{font-size:12px;}
        div{font-size:12px;}
![]() </style>
    </style>
![]() </head>
</head>
![]() <body>
<body>
![]() <form id="form1" runat="server">
    <form id="form1" runat="server">
![]() <h3>Class reflection sample</h3>
    <h3>Class reflection sample</h3>
![]() <div>
    <div>
![]() <asp:Label runat="server" ID="ProperList">
    <asp:Label runat="server" ID="ProperList">
![]() </asp:Label>
    </asp:Label>
![]() <hr />
    <hr />
![]() <asp:Label runat="server" ID="MethodList">
    <asp:Label runat="server" ID="MethodList">
![]() </asp:Label>
    </asp:Label>
![]() <hr />
    <hr />
![]() <asp:Label runat="server" ID="EventList">
    <asp:Label runat="server" ID="EventList">
![]() </asp:Label>
    </asp:Label>
![]() <hr />
    <hr />
![]() </div>
    </div>
![]() </form>
    </form>
![]() </body>
</body>
![]() </html>
</html>
![]()
Reflection.aspx.cs
![]() using System;
using System;
![]() using System.Data;
using System.Data;
![]() using System.Configuration;
using System.Configuration;
![]() using System.Collections;
using System.Collections;
![]() using System.Web;
using System.Web;
![]() using System.Web.Security;
using System.Web.Security;
![]() using System.Web.UI;
using System.Web.UI;
![]() using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
![]() using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts;
![]() using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;
![]() using System.Reflection;
using System.Reflection;
![]()
![]() public partial class Reflection : System.Web.UI.Page
public partial class Reflection : System.Web.UI.Page
![]() {
{
![]() protected void Page_Load(object sender, EventArgs ea)
    protected void Page_Load(object sender, EventArgs ea)
![]() {
    {
![]() //Type t = Type.GetType("System.Web.UI.WebControls.LinkButton");
        //Type t = Type.GetType("System.Web.UI.WebControls.LinkButton");
![]()
![]() Type t = typeof(LinkButton);
        Type t = typeof(LinkButton);
![]()
![]() ConstructorInfo ci = t.GetConstructor(new Type[] { }); //获取当前 Type 的特定构造函数。
        ConstructorInfo ci = t.GetConstructor(new Type[] { }); //获取当前 Type 的特定构造函数。
![]() object o = ci.Invoke(null); //调用该实例反映的构造函数
        object o = ci.Invoke(null); //调用该实例反映的构造函数
![]() 
        
![]() 
        
![]() {
        {
![]() PropertyInfo p = t.GetProperty("Text"); //获取当前 Type 的特定属性
            PropertyInfo p = t.GetProperty("Text"); //获取当前 Type 的特定属性
![]() p.SetValue(o, "Hello world", null);
            p.SetValue(o, "Hello world", null);
![]() }
        }
![]()
![]() PropertyInfo[] propers = t.GetProperties();//获取当前 Type 的属性。
        PropertyInfo[] propers = t.GetProperties();//获取当前 Type 的属性。
![]()
![]() foreach (PropertyInfo p in propers)
        foreach (PropertyInfo p in propers)
![]() {
        {
![]() ProperList.Text += p.Name + " = " + p.GetValue(o, null) + "<br/>";
            ProperList.Text += p.Name + " = " + p.GetValue(o, null) + "<br/>";
![]() }
        }
![]()
![]() //方法
        //方法
![]() MethodInfo[] methods = t.GetMethods();
        MethodInfo[] methods = t.GetMethods();
![]()
![]() foreach (MethodInfo m in methods)
        foreach (MethodInfo m in methods)
![]() {
        {
![]() if (m.IsPublic == true)
            if (m.IsPublic == true)
![]() {
            {
![]() MethodList.Text += " public ";
                MethodList.Text += " public ";
![]() }
            }
![]()
![]() if (m.IsVirtual == true)
            if (m.IsVirtual == true)
![]() {
            {
![]() MethodList.Text += " virtual ";
                MethodList.Text += " virtual ";
![]() }
            }
![]() if (m.IsStatic == true)
            if (m.IsStatic == true)
![]() {
            {
![]() MethodList.Text += " static ";
                MethodList.Text += " static ";
![]() }
            }
![]()
![]() MethodList.Text += "<span style=\"color:blue\">" + m.ReturnType.ToString() + "</span> " + m.Name + " (";
            MethodList.Text += "<span style=\"color:blue\">" + m.ReturnType.ToString() + "</span> " + m.Name + " (";
![]() ParameterInfo[] paras = m.GetParameters();
            ParameterInfo[] paras = m.GetParameters();
![]()
![]() bool IsParameter = false;
            bool IsParameter = false;
![]()
![]() foreach (ParameterInfo pa in paras)
            foreach (ParameterInfo pa in paras)
![]() {
            {
![]() MethodList.Text += "<span style=\"color:blue\">" + pa.ParameterType.ToString() + "</span> " + pa.Name + ",";
                MethodList.Text += "<span style=\"color:blue\">" + pa.ParameterType.ToString() + "</span> " + pa.Name + ",";
![]()
![]() IsParameter = true;
                IsParameter = true;
![]() }
            }
![]()
![]() if (IsParameter == true)
            if (IsParameter == true)
![]() {
            {
![]() MethodList.Text = MethodList.Text.Substring(0, MethodList.Text.Length - 1);
                MethodList.Text = MethodList.Text.Substring(0, MethodList.Text.Length - 1);
![]() }
            }
![]()
![]() MethodList.Text += ")<br/>";
            MethodList.Text += ")<br/>";
![]() }
        }
![]()
![]() //事件
        //事件
![]() EventInfo[] events = t.GetEvents();
        EventInfo[] events = t.GetEvents();
![]()
![]() foreach (EventInfo e in events)
        foreach (EventInfo e in events)
![]() {
        {
![]() EventList.Text += e.Name;
            EventList.Text += e.Name;
![]()
![]() EventList.Text += "<br/>";
            EventList.Text += "<br/>";
![]() }
        }
![]()
![]() this.form1.Controls.Add((Control)o);
        this.form1.Controls.Add((Control)o);
![]() }
    }
![]() }
}
![]()
结果
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reflection.aspx.cs" Inherits="Reflection" %>
<%@ 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">
<!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" >
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<head runat="server"> <title>Class reflection sample</title>
    <title>Class reflection sample</title> <style>
    <style> div{font-size:12px;}
        div{font-size:12px;} </style>
    </style> </head>
</head> <body>
<body> <form id="form1" runat="server">
    <form id="form1" runat="server"> <h3>Class reflection sample</h3>
    <h3>Class reflection sample</h3> <div>
    <div> <asp:Label runat="server" ID="ProperList">
    <asp:Label runat="server" ID="ProperList"> </asp:Label>
    </asp:Label> <hr />
    <hr /> <asp:Label runat="server" ID="MethodList">
    <asp:Label runat="server" ID="MethodList"> </asp:Label>
    </asp:Label> <hr />
    <hr /> <asp:Label runat="server" ID="EventList">
    <asp:Label runat="server" ID="EventList"> </asp:Label>
    </asp:Label> <hr />
    <hr /> </div>
    </div> </form>
    </form> </body>
</body> </html>
</html>
Reflection.aspx.cs
 using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Collections;
using System.Collections; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls; using System.Reflection;
using System.Reflection;
 public partial class Reflection : System.Web.UI.Page
public partial class Reflection : System.Web.UI.Page {
{ protected void Page_Load(object sender, EventArgs ea)
    protected void Page_Load(object sender, EventArgs ea) {
    { //Type t = Type.GetType("System.Web.UI.WebControls.LinkButton");
        //Type t = Type.GetType("System.Web.UI.WebControls.LinkButton");
 Type t = typeof(LinkButton);
        Type t = typeof(LinkButton);
 ConstructorInfo ci = t.GetConstructor(new Type[] { }); //获取当前 Type 的特定构造函数。
        ConstructorInfo ci = t.GetConstructor(new Type[] { }); //获取当前 Type 的特定构造函数。 object o = ci.Invoke(null); //调用该实例反映的构造函数
        object o = ci.Invoke(null); //调用该实例反映的构造函数 
         
         {
        { PropertyInfo p = t.GetProperty("Text"); //获取当前 Type 的特定属性
            PropertyInfo p = t.GetProperty("Text"); //获取当前 Type 的特定属性 p.SetValue(o, "Hello world", null);
            p.SetValue(o, "Hello world", null); }
        }
 PropertyInfo[] propers = t.GetProperties();//获取当前 Type 的属性。
        PropertyInfo[] propers = t.GetProperties();//获取当前 Type 的属性。
 foreach (PropertyInfo p in propers)
        foreach (PropertyInfo p in propers) {
        { ProperList.Text += p.Name + " = " + p.GetValue(o, null) + "<br/>";
            ProperList.Text += p.Name + " = " + p.GetValue(o, null) + "<br/>"; }
        }
 //方法
        //方法 MethodInfo[] methods = t.GetMethods();
        MethodInfo[] methods = t.GetMethods();
 foreach (MethodInfo m in methods)
        foreach (MethodInfo m in methods) {
        { if (m.IsPublic == true)
            if (m.IsPublic == true) {
            { MethodList.Text += " public ";
                MethodList.Text += " public "; }
            }
 if (m.IsVirtual == true)
            if (m.IsVirtual == true) {
            { MethodList.Text += " virtual ";
                MethodList.Text += " virtual "; }
            } if (m.IsStatic == true)
            if (m.IsStatic == true) {
            { MethodList.Text += " static ";
                MethodList.Text += " static "; }
            }
 MethodList.Text += "<span style=\"color:blue\">" + m.ReturnType.ToString() + "</span> " + m.Name + " (";
            MethodList.Text += "<span style=\"color:blue\">" + m.ReturnType.ToString() + "</span> " + m.Name + " ("; ParameterInfo[] paras = m.GetParameters();
            ParameterInfo[] paras = m.GetParameters();
 bool IsParameter = false;
            bool IsParameter = false;
 foreach (ParameterInfo pa in paras)
            foreach (ParameterInfo pa in paras) {
            { MethodList.Text += "<span style=\"color:blue\">" + pa.ParameterType.ToString() + "</span> " + pa.Name + ",";
                MethodList.Text += "<span style=\"color:blue\">" + pa.ParameterType.ToString() + "</span> " + pa.Name + ",";
 IsParameter = true;
                IsParameter = true; }
            }
 if (IsParameter == true)
            if (IsParameter == true) {
            { MethodList.Text = MethodList.Text.Substring(0, MethodList.Text.Length - 1);
                MethodList.Text = MethodList.Text.Substring(0, MethodList.Text.Length - 1); }
            }
 MethodList.Text += ")<br/>";
            MethodList.Text += ")<br/>"; }
        }
 //事件
        //事件 EventInfo[] events = t.GetEvents();
        EventInfo[] events = t.GetEvents();
 foreach (EventInfo e in events)
        foreach (EventInfo e in events) {
        { EventList.Text += e.Name;
            EventList.Text += e.Name;
 EventList.Text += "<br/>";
            EventList.Text += "<br/>"; }
        }
 this.form1.Controls.Add((Control)o);
        this.form1.Controls.Add((Control)o); }
    } }
}
结果
 
                    
                 


 div
        div 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号