码迷,mamicode.com
首页 > 其他好文 > 详细

简单的反射Demo

时间:2015-01-12 16:20:03      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 1  private void button1_Click(object sender, EventArgs e)
 2         {//计时器
 3             System.Diagnostics.Stopwatch watcher = new System.Diagnostics.Stopwatch();
 4             //开启计时器
 5             watcher.Start();
 6             Users user = new Users();
 7             user.Name = "sa";
 8             user.Password = "123";
 9             user.Time = DateTime.Now;
10             BingData<Users>(user);
11             //结束计时器
12             watcher.Stop();
13             MessageBox.Show("毫秒:" + watcher.Elapsed.TotalMilliseconds.ToString());
14             MessageBox.Show("秒:" + watcher.Elapsed.TotalSeconds.ToString());
15 
16 
17         }
18 
19         private void BingData<T>(T model)
20         {
21             //获取类型
22             Type t = typeof(T);
23             //获取类型的所有属性
24             System.Reflection.PropertyInfo[] pros = t.GetProperties();
25             foreach (var item in pros)
26             {
27                 foreach (Control c in this.Controls)
28                 {
29                     if (c.Name.Contains("txt_" + item.Name))
30                     {//获取属性值
31                         Object obj = item.GetValue(model);
32                         c.Text = obj.ToString();
33                     }
34                 }
35             }
36         }

 

 

简单的反射Demo

标签:

原文地址:http://www.cnblogs.com/junhuang/p/4218427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!