通过反射获取类的所有属性和方法

本文介绍了一种使用反射机制来获取.NET Framework中类的所有属性和方法的技术。具体实现包括获取类名、所有公共方法及所有属性,并展示了如何在用户界面上展示这些信息。
 1 None.gif      private   void  button1_Click( object  sender, EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            Type t = typeof(System.Drawing.Color);
 4InBlock.gif            string className = t.Name;
 5InBlock.gif            MessageBox.Show(className);
 6InBlock.gif
 7InBlock.gif            //获取所有方法
 8InBlock.gif            System.Reflection.MethodInfo[] methods = t.GetMethods();
 9InBlock.gif            this.textBox1.Text = "";
10InBlock.gif            foreach (System.Reflection.MethodInfo method in methods)
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                this.textBox1.Text += method.Name + System.Environment.NewLine;
13ExpandedSubBlockEnd.gif            }

14InBlock.gif
15InBlock.gif            //获取所有成员
16InBlock.gif            System.Reflection.MemberInfo[] members = t.GetMembers();
17InBlock.gif
18InBlock.gif            //获取所有属性
19InBlock.gif            System.Reflection.PropertyInfo[] properties = t.GetProperties();
20InBlock.gif            foreach (System.Reflection.PropertyInfo property in properties)
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                this.lstColors.Items.Add(property.Name);
23ExpandedSubBlockEnd.gif            }

24ExpandedBlockEnd.gif        }

25 None.gif
26 None.gif         private   void  lstColors_SelectedIndexChanged( object  sender, EventArgs e)
27 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
28InBlock.gif           this.pictureBox1.BackColor=  System.Drawing.Color.FromName(((ListBox)sender).Text);
29ExpandedBlockEnd.gif        }

30 None.gif
posted on 2008-03-28 16:09 guofu 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/saptechnique/archive/2008/03/28/1127834.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值