一个反射的实例

 分成三步

1、using System.Reflection;

2、Type _type=你的对象.GetType();

3、遍历操作你想要的东西

如:ConstructorInfo、EventInfo、FiledInfo、InterfaceInfo、MemberInfo、MethodInfo、PropertyInfo

本实例就用FiledInfo、MemberInfo、MethodInfo、PropertyInfo四个

 

 

添加一个用于测试的类PersonClass

public class PersonClass
    {
        public string aa;
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        private int tel;

        public int Tel
        {
            get { return tel; }
            set { tel = value; }
        }

        public PersonClass()
        {
            
        }

        public PersonClass(string name,string sex,int age,int Tel)
        {
            this.Name = name;
            Sex = sex;
            this.Age = age;
            this.Tel = Tel;
        }

        protected void SetAge(int age)
        {
            this.Age = age;
        }

        public  void PrintName()
        {
            System.Web.HttpContext.Current.Response.Write("xyt");
        }
    }


 

 

default.aspx上放四个TextBox

<div>
    
        <asp:TextBox ID="TextBox1" TextMode= "MultiLine" runat="server"></asp:TextBox>
    
        <br />
        <br />
    
        <asp:TextBox ID="TextBox2" TextMode= "MultiLine" runat="server"></asp:TextBox>
    
        <br />
        <br />
    
        <asp:TextBox ID="TextBox3" TextMode= "MultiLine" runat="server" Height="223px"></asp:TextBox>
    
        <br />
        <br />
    
        <asp:TextBox ID="TextBox4" TextMode= "MultiLine" runat="server" Height="330px" 
            Width="456px"></asp:TextBox>
    
    </div>


 

 

 

default.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Reflection;


namespace MyReflection
{
    public partial class _Default : System.Web.UI.Page
    {
        PersonClass _Person=new PersonClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetPropertyInfo();
            GetFilesInfo();
            GetMethodInfo();
            GetMemberInfo();
        }
        public void GetPropertyInfo()
        {
            Type _type=_Person.GetType();

            foreach (PropertyInfo item in _type.GetProperties())
            {
                this.TextBox1.Text += item.Name.ToString() + "\n";
            }
        }

        public void GetFilesInfo()
        {
            Type type = _Person.GetType();
            foreach (FieldInfo item in type.GetFields())
            {
                this.TextBox2.Text += item.Name.ToString() + "\n";
            }
        }

        public void GetMethodInfo()
        {
            Type type = _Person.GetType();
            foreach (MethodInfo item in type.GetMethods())
            {
                this.TextBox3.Text += item.Name.ToString() + "\n";
            }
        }

        public void GetMemberInfo()
        {
            Type type = _Person.GetType();
            foreach (MemberInfo item in type.GetMembers())
            {
                this.TextBox4.Text += item.DeclaringType+" "+item.MemberType+" "+item.Name.ToString() +" "+ "\n";
            }
        }
    }
}


注:public string aa;虽然没有get、set,通过GetProperties获取不到,但可以通过GetFields来获取

  上面的方法有重载的形式:BindingFlags枚举的值,表示应返回那些成员

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值