需将容器中的控件名称改成与实体属性一致,由程序做匹配。以下代码还不是很完善,扩展性也不是很好。 先放着了。 using System; using System.Reflection; using System.Configuration.Assemblies; using System.Collections.Generic; #region 获取对象属性类,使用TabControl 容器 /// <summary> /// 获取对象属性类 /// </summary> /// <typeparam name="T">实体</typeparam> public class GetObjectAttute<T> where T : new() { public static List<T> SaveSelectedTabPage(TabControl tabPage) { T t = new T(); Type type = typeof(T); List<T> list = new List<T>(); FieldInfo[] fieldInfo = type.GetFields(); object obj = Assembly.Load("DDL.Entities").CreateInstance("DDL.Entities." + typeof(T).Name); if (obj != null) { PropertyInfo[] propertyInfo = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); //遍历属性集合 for (int i = 0; i < propertyInfo.Length; i++) { //遍历TabControl获取TabPage foreach (TabPage tabpage in tabPage.TabPages) { //遍历TabPage中控件的集合 foreach (Control control in tabpage.Controls) { if (control != null) { //遍历实体的所有公共字段 foreach (FieldInfo fieldinfo in fieldInfo) { if (propertyInfo[i].Name != "Id") { if (control.Name.IndexOf(propertyInfo[i].Name) > 0) { if (propertyInfo[i].PropertyType.Name == typeof(Int32).Name) { propertyInfo[i].SetValue(t, CommonFunction.CheckInt(control.Text), null); } else if (propertyInfo[i].PropertyType.Name == typeof(DateTime).Name) { propertyInfo[i].SetValue(t, DateTimeFunction.CheackDateTime(control.Text), null); } else { if (control is TextBox) { TextBox txtcbox = control as TextBox; if (txtcbox != null) { propertyInfo[i].SetValue(t, txtcbox.Text, null); } } } } } } } } } } list.Add(t); } return list; } } #endregion