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 }