Goodspeed

导航

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);
    }

}


结果

Class reflection sample

Text = Hello world
CommandName =
CommandArgument =
CausesValidation = True
OnClientClick =
PostBackUrl =
ValidationGroup =
AccessKey =
Attributes = System.Web.UI.AttributeCollection
BackColor = Color [Empty]
BorderColor = Color [Empty]
BorderWidth =
BorderStyle = NotSet
ControlStyle = System.Web.UI.WebControls.Style
ControlStyleCreated = True
CssClass =
Style = System.Web.UI.CssStyleCollection
Enabled = True
EnableTheming = True
Font =
ForeColor = Color [Empty]
HasAttributes = False
Height =
SkinID =
TabIndex = 0
ToolTip =
Width =
ClientID =
ID =
EnableViewState = True
NamingContainer =
BindingContainer =
Page =
TemplateControl =
Parent =
TemplateSourceDirectory = /myaspx
AppRelativeTemplateSourceDirectory = /myaspx
Site =
Visible = True
UniqueID =
Controls = System.Web.UI.ControlCollection

public virtual System.String get_Text ()
public virtual System.Void set_Text (System.String value)
public virtual System.Void set_CommandName (System.String value)
public virtual System.String get_CommandName ()
public virtual System.Void set_CommandArgument (System.String value)
public virtual System.String get_CommandArgument ()
public virtual System.Boolean get_CausesValidation ()
public virtual System.Void set_CausesValidation (System.Boolean value)
public virtual System.String get_OnClientClick ()
public virtual System.Void set_OnClientClick (System.String value)
public virtual System.String get_PostBackUrl ()
public virtual System.Void set_PostBackUrl (System.String value)
public virtual System.String get_ValidationGroup ()
public virtual System.Void set_ValidationGroup (System.String value)
public virtual System.String get_AccessKey ()
public virtual System.Void set_AccessKey (System.String value)
public System.Web.UI.AttributeCollection get_Attributes ()
public virtual System.Void set_BackColor (System.Drawing.Color value)
public virtual System.Drawing.Color get_BackColor ()
public virtual System.Drawing.Color get_BorderColor ()
public virtual System.Void set_BorderColor (System.Drawing.Color value)
public virtual System.Web.UI.WebControls.Unit get_BorderWidth ()
public virtual System.Void set_BorderWidth (System.Web.UI.WebControls.Unit value)
public virtual System.Void set_BorderStyle (System.Web.UI.WebControls.BorderStyle value)
public virtual System.Web.UI.WebControls.BorderStyle get_BorderStyle ()
public System.Web.UI.WebControls.Style get_ControlStyle ()
public System.Boolean get_ControlStyleCreated ()
public virtual System.Void set_CssClass (System.String value)
public virtual System.String get_CssClass ()
public System.Web.UI.CssStyleCollection get_Style ()
public virtual System.Boolean get_Enabled ()
public virtual System.Void set_Enabled (System.Boolean value)
public virtual System.Boolean get_EnableTheming ()
public virtual System.Void set_EnableTheming (System.Boolean value)
public virtual System.Web.UI.WebControls.FontInfo get_Font ()
public virtual System.Void set_ForeColor (System.Drawing.Color value)
public virtual System.Drawing.Color get_ForeColor ()
public System.Boolean get_HasAttributes ()
public virtual System.Web.UI.WebControls.Unit get_Height ()
public virtual System.Void set_Height (System.Web.UI.WebControls.Unit value)
public virtual System.String get_SkinID ()
public virtual System.Void set_SkinID (System.String value)
public virtual System.Void set_TabIndex (System.Int16 value)
public virtual System.Int16 get_TabIndex ()
public virtual System.String get_ToolTip ()
public virtual System.Void set_ToolTip (System.String value)
public virtual System.Void set_Width (System.Web.UI.WebControls.Unit value)
public virtual System.Web.UI.WebControls.Unit get_Width ()
public virtual System.String get_ClientID ()
public virtual System.Void set_ID (System.String value)
public virtual System.String get_ID ()
public virtual System.Void set_EnableViewState (System.Boolean value)
public virtual System.Boolean get_EnableViewState ()
public virtual System.Web.UI.Control get_NamingContainer ()
public System.Web.UI.Control get_BindingContainer ()
public virtual System.Web.UI.Page get_Page ()
public virtual System.Void set_Page (System.Web.UI.Page value)
public System.Web.UI.TemplateControl get_TemplateControl ()
public System.Void set_TemplateControl (System.Web.UI.TemplateControl value)
public virtual System.Web.UI.Control get_Parent ()
public virtual System.String get_TemplateSourceDirectory ()
public System.String get_AppRelativeTemplateSourceDirectory ()
public System.Void set_AppRelativeTemplateSourceDirectory (System.String value)
public virtual System.Void set_Site (System.ComponentModel.ISite value)
public virtual System.ComponentModel.ISite get_Site ()
public virtual System.Void set_Visible (System.Boolean value)
public virtual System.Boolean get_Visible ()
public virtual System.String get_UniqueID ()
public virtual System.Web.UI.ControlCollection get_Controls ()
public virtual System.Void add_Click (System.EventHandler value)
public virtual System.Void remove_Click (System.EventHandler value)
public virtual System.Void add_Command (System.Web.UI.WebControls.CommandEventHandler value)
public virtual System.Void remove_Command (System.Web.UI.WebControls.CommandEventHandler value)
public virtual System.Void RenderBeginTag (System.Web.UI.HtmlTextWriter writer)
public virtual System.Void RenderEndTag (System.Web.UI.HtmlTextWriter writer)
public System.Void ApplyStyle (System.Web.UI.WebControls.Style s)
public System.Void CopyBaseAttributes (System.Web.UI.WebControls.WebControl controlSrc)
public System.Void MergeStyle (System.Web.UI.WebControls.Style s)
public virtual System.Void add_Disposed (System.EventHandler value)
public virtual System.Void remove_Disposed (System.EventHandler value)
public virtual System.Void ApplyStyleSheetSkin (System.Web.UI.Page page)
public virtual System.Void DataBind ()
public virtual System.Void Focus ()
public virtual System.Void RenderControl (System.Web.UI.HtmlTextWriter writer)
public virtual System.Void Dispose ()
public virtual System.String ResolveClientUrl (System.String relativeUrl)
public virtual System.Web.UI.Control FindControl (System.String id)
public virtual System.Boolean HasControls ()
public System.Void add_DataBinding (System.EventHandler value)
public System.Void remove_DataBinding (System.EventHandler value)
public System.Void add_Init (System.EventHandler value)
public System.Void remove_Init (System.EventHandler value)
public System.Void add_Load (System.EventHandler value)
public System.Void remove_Load (System.EventHandler value)
public System.Void add_PreRender (System.EventHandler value)
public System.Void remove_PreRender (System.EventHandler value)
public System.Void add_Unload (System.EventHandler value)
public System.Void remove_Unload (System.EventHandler value)
public System.String ResolveUrl (System.String relativeUrl)
public System.Void SetRenderMethodDelegate (System.Web.UI.RenderMethod renderMethod)
public System.Type GetType ()
public virtual System.String ToString ()
public virtual System.Boolean Equals (System.Object obj)
public virtual System.Int32 GetHashCode ()

Click
Command
Disposed
DataBinding
Init
Load
PreRender
Unload

Hello world

posted on 2005-07-21 14:18  Goodspeed  阅读(1889)  评论(0编辑  收藏  举报