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;  // MethodInfo 需要引用
namespace Example23
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Text = "使用 typeof 关键字获取类的内部结构";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Type type = typeof(System.Int32);  //获得 int32 类型的Type对象
            foreach (MethodInfo method in type.GetMethods()) //遍历 int32 类中所有的公共方法
            {
                richTextBox1.AppendText("方法名称:" + method.Name + Environment.NewLine); //输出方法名称
                foreach (ParameterInfo parameter in method.GetParameters())  //遍历 公共方法 中的 所有参数
                {
                    richTextBox1.AppendText(" 参数:" + parameter.Name + Environment.NewLine); //输出参数名称
                }
            }
            //typeof 另类用法 
            foreach (Control c in Controls) //遍历窗体控件集合 
            {
                if (c.GetType() == typeof(TextBox)) // 判断是否 TextBox 控件
                {
                    ((TextBox)c).Clear();   //清空 控件 内容
                }
            }
        }
    }
}
posted on 2011-10-17 01:27  C#_初学者  阅读(215)  评论(0编辑  收藏  举报