/// 加入子集内容 private T GetSingle(T t) { T sg = t; //FindByCondition(t)[0]; if (sg == null) { return(null); } ProList = Type_Table.GetProperties(); foreach (PropertyInfo Pro in ProList) { object[] objAttrs = Pro.GetCustomAttributes(typeof(DataMapAttribute), true); if (objAttrs.Length > 0) { DataMapAttribute attr = objAttrs[0] as DataMapAttribute; if (attr.BelongsTo != null) { //带出关联实体。。 string key = attr.BelongsToKey; PropertyInfo bttpro = Type_Table.GetProperty(key); string tkey = attr.TargetKey; string kvalue = bttpro.GetValue(sg, null).ToString(); //根据key获取值 int kv = 0; Type btt = Type.GetType(Pro.GetGetMethod().ReturnType.FullName); DataMapAttribute table = btt.GetCustomAttributes(typeof(DataMapAttribute), true)[0] as DataMapAttribute; PropertyInfo[] bttProList = btt.GetProperties(); string sql = "SELECT "; foreach (PropertyInfo sgPro in bttProList) { object[] sgobjAttrs = sgPro.GetCustomAttributes(typeof(DataMapAttribute), true); if (sgobjAttrs.Length > 0) { DataMapAttribute sgattr = sgobjAttrs[0] as DataMapAttribute; if (sgattr.Column != null) { sql += sgattr.Column + ","; } } } sql = sql.Substring(0, sql.Length - 1); sql += " FROM " + table.TableName + " WHERE 1=1"; //Type_Table.Name; if (int.TryParse(kvalue, out kv)) { sql += " AND " + tkey + "=" + kvalue; } else { sql += " AND " + tkey + "='" + kvalue + "'"; } DataTable dt = db.GetDataTable(sql); var makeme = System.Type.GetType(btt.FullName); object o = Activator.CreateInstance(makeme); Type oot = o.GetType(); PropertyInfo[] ootProList = oot.GetProperties(); if (ootProList.Count() > 0) { for (int i = 0; i < ootProList.Count(); i++) { PropertyInfo ootpro = ootProList[i]; if (TypeRead.TRead(ootpro.PropertyType.FullName) == "Int32") { ootpro.SetValue(o, Convert.ToInt32(string.IsNullOrEmpty(dt.Rows[0][i].ToString()) ? "0" : dt.Rows[0][i].ToString()), null); } else { ootpro.SetValue(o, dt.Rows[0][i].ToString(), null); } } } Pro.SetValue(sg, o, null); } } } return(sg); }
/// 加入子集内容 private T GetSingle(T t) { try { T sg = t; //FindByCondition(t)[0]; if (sg == null) { return(null); } ProList = Type_Table.GetProperties(); foreach (PropertyInfo Pro in ProList) //USER { object[] objAttrs = Pro.GetCustomAttributes(typeof(DataMapAttribute), true); if (objAttrs.Length > 0) { DataMapAttribute attr = objAttrs[0] as DataMapAttribute; if (attr.BelongsTo != null) { //带出关联实体。。 string key = attr.BelongsToKey; PropertyInfo bttpro = Type_Table.GetProperty(key); string tkey = attr.TargetKey; string kvalue = bttpro.GetValue(sg, null).ToString(); //根据key获取值 int kv = 0; Type btt = Type.GetType(Pro.GetGetMethod().ReturnType.FullName); DataMapAttribute table = btt.GetCustomAttributes(typeof(DataMapAttribute), true)[0] as DataMapAttribute; PropertyInfo[] bttProList = btt.GetProperties(); string sql = "SELECT "; foreach (PropertyInfo sgPro in bttProList) { object[] sgobjAttrs = sgPro.GetCustomAttributes(typeof(DataMapAttribute), true); if (sgobjAttrs.Length > 0) { DataMapAttribute sgattr = sgobjAttrs[0] as DataMapAttribute; if (sgattr.Column != null) { sql += sgattr.Column + ","; } } } sql = sql.Substring(0, sql.Length - 1); sql += " FROM " + table.TableName + " WHERE 1=1"; //Type_Table.Name; if (int.TryParse(kvalue, out kv)) { sql += " AND " + tkey + "=" + kvalue; } else { sql += " AND " + tkey + "='" + kvalue + "'"; } DataTable dt = db.GetDataTable(sql); if (dt.Rows.Count < 1) { continue; } var makeme = System.Type.GetType(btt.FullName); object o = Activator.CreateInstance(makeme); Type oot = o.GetType(); PropertyInfo[] ootProList = oot.GetProperties(); if (ootProList.Count() > 0) { for (int i = 0; i < ootProList.Count(); i++) //USER-ROLE { PropertyInfo ootpro = ootProList[i]; if (TypeRead.TRead(ootpro.PropertyType.FullName) == "Int32") { ootpro.SetValue(o, Convert.ToInt32(string.IsNullOrEmpty(dt.Rows[0][i].ToString()) ? "0" : dt.Rows[0][i].ToString()), null); } else if (TypeRead.TRead(ootpro.PropertyType.FullName) == "Null") { //查看是否有下钻或多对多关系 object[] objAttrs_r = ootpro.GetCustomAttributes(typeof(DataMapAttribute), true); DataMapAttribute attr_r = objAttrs_r[0] as DataMapAttribute; if (attr_r.HasMany) { string t_table = attr_r.ManyTableName; string self_col = attr_r.ManyCloumnSelf; string other_col = attr_r.ManyClounmOther; Type btt_r = Type.GetType(ootProList[i].GetGetMethod().ReturnType.FullName); //大类型 var makeme_r = System.Type.GetType(btt_r.FullName); object o_r = Activator.CreateInstance(makeme_r); o_r = GetManyData(o_r.GetType(), t_table, self_col, other_col); ootpro.SetValue(o, o_r, null); } } else { Type type = ootpro.PropertyType; if (type.FullName.Contains("Nullable")) { NullableConverter nullableConverter = new NullableConverter(type); type = nullableConverter.UnderlyingType; string v = dt.Rows[0][i].ToString(); if (type.Name.Contains("DateTime")) { v = string.IsNullOrEmpty(v) ? DateTime.MinValue.ToString() : v; } if (type.Name.Contains("Int")) { v = string.IsNullOrEmpty(v) ? int.MinValue.ToString() : v; } ootpro.SetValue(o, Convert.ChangeType(v, type), null); } else { ootpro.SetValue(o, dt.Rows[0][i].ToString(), null); } } } } Pro.SetValue(sg, o, null); } //多对多的关系 if (attr.HasMany) { string t_table = attr.ManyTableName; string self_col = attr.ManyCloumnSelf; string other_col = attr.ManyClounmOther; Type btt = Type.GetType(Pro.GetGetMethod().ReturnType.FullName); //大类型 var makeme = System.Type.GetType(btt.FullName); object o = Activator.CreateInstance(makeme); o = GetManyData(o.GetType(), t_table, self_col, other_col); Pro.SetValue(sg, o, null); } } } return(sg); } catch (Exception e) { throw e; } }