反射机制访问对象类型——访问某对象所有属性(自己写的例子)

在窗体程序中添加一个类文件Student(解决方案右键->添加->类 类名Student。 )

类的属性必须要有get set方法。(此处注意类的属性与字段的区别)

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseReflection1
{
    public class Student
    {
        private string _name;//字段
        public string name //属性必须要有get set
        {
            get { return _name; }
            set { _name = value; }
        }

        private int _age;
        public int age 
        {
            get { return _age; }
            set { _age = value; }
        }

        private string _sex;
        public string sex
        {
            get { return _sex; }
            set { _sex = value; }
        }
    }
}

Form代码如下:

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace UseReflection1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Student stu = new Student();//实例化对象
            stu.name = "张三";
            stu.age = 24;
            stu.sex = "";

            excute(stu);  //类的实体反射得到所有属性值          
        }
        #region
        /// <summary>
        /// 类的实体反射得到所有属性名和属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="str"></param>
        public void excute(object obj)
        {
            try
            {
                Type objtype = obj.GetType();//得到对象的类型
                PropertyInfo[] property = objtype.GetProperties();//得到实体中所有属性
                Label[] label = new Label[property.GetLength(0)];//定义一个长度和实体属性个数相等的Label数组
                TextBox[] TxtBx_Value = new TextBox[property.GetLength(0)];//定义一个长度和实体属性个数相等的TextBox数组
                int index = 0;//数组下标
                foreach (PropertyInfo pro in property)//遍历属性,将属性名称及属性值显示到窗体。
                {
                    Label lb = new Label();
                    lb.Text = pro.Name.ToString();//属性名称
                    lb.Size = new Size(50, 20);
                    lb.Location = new Point(50, 100 + index*30);
                    this.Controls.Add(lb);

                    TextBox tb = new TextBox();
                    tb.Location =new Point(100,100+ index*30);
                    tb.Text = Convert.ToString(pro.GetValue(obj, null));//属性值
                    this.Controls.Add(tb);
                    //动态创建label和textbox显示信息



                    ///form窗体中已经有多个label和textbox,此处循环遍历给他们赋值显示  与上面效果一样
                    //(Controls["Label" + (index + 4).ToString()] as Label).Text = pro.Name.ToString();//属性名称
                    //(Controls["TxtBx_value" + index.ToString()] as TextBox).Text = Convert.ToString(pro.GetValue(obj, null));//属性值
                    
                    index++;//数组下班加一
                }
            }
            catch
            { return; }
        }
        #endregion
    }
}


结果:

posted @ 2013-04-03 11:15  sevennight99  阅读(239)  评论(0编辑  收藏  举报