DT转实体
Posted yzm97
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DT转实体相关的知识,希望对你有一定的参考价值。
1 #region SqlDataReader 转实体 2 /// <summary> 3 /// SqlDataReader 转实体 4 /// </summary> 5 /// <typeparam name="T"></typeparam> 6 /// <param name="row"></param> 7 /// <returns></returns> 8 private T SqlDataReaderToModel<T>(DataRow row) 9 { 10 try 11 { 12 T model; 13 var type = typeof(T); 14 model = Activator.CreateInstance<T>(); 15 var modelPropertyInfos = type.GetProperties(); 16 //遍历model每一个属性并赋值SqlDataReader对应的列 17 foreach (var pop in modelPropertyInfos) 18 { 19 try 20 { 21 //获取属性名称 22 var name = pop.Name; 23 if (pop.IsDefined(typeof(NameconversionAttr), true))//判断属性成员中是否有此特性包括继承过来的特性 24 { 25 26 foreach (NameconversionAttr attribute in pop.GetCustomAttributes(typeof(NameconversionAttr), true))//实例特性 27 { 28 name = attribute.GetName(); 29 } 30 if (name.Contains("序号")) 31 { 32 continue; 33 } 34 else if (name.Contains("分类")) 35 { 36 var value = db.DeviceCategory.ToList(); 37 var aname = (string)row[name]; 38 row.BeginEdit();//开始编辑行 39 row[name] = (int)value.Where(x => x.DeviceCategoryName == aname).FirstOrDefault().Id;//给行的列"columnname"赋值 40 row.EndEdit();//结束编辑 41 //建议值 42 43 } 44 } 45 var popType = GetModelType(pop.PropertyType); 46 switch (popType) 47 { 48 case ModelType.Struct: 49 { 50 var fiType = pop.PropertyType; 51 if (fiType == typeof(DateTime)) 52 { 53 pop.SetValue(model, Convert.ToDateTime(row[name])); 54 } 55 else if (fiType == typeof(Int32)) 56 { 57 pop.SetValue(model, Convert.ToInt32(row[name])); 58 } 59 else if (fiType == typeof(decimal)) 60 { 61 var s = row[name]; 62 if (string.IsNullOrWhiteSpace(s.ToString())) 63 { 64 pop.SetValue(model, 0.0m); 65 } 66 else 67 { 68 pop.SetValue(model, Convert.ToDecimal(row[name])); 69 } 70 } 71 else 72 { 73 pop.SetValue(model, row[name] == DBNull.Value ? null : row[name]); 74 } 75 } 76 break; 77 case ModelType.Enum: 78 { 79 var fiType = pop.PropertyType; 80 if (fiType == typeof(int)) 81 { 82 pop.SetValue(model, row[name]); 83 } 84 else if (fiType == typeof(string)) 85 { 86 var value = (T)Enum.Parse(typeof(T), row[name].ToString()); 87 if (value != null) 88 pop.SetValue(model, value); 89 } 90 else if (fiType == typeof(DateTime)) 91 { 92 var value = (T)Enum.Parse(typeof(T), row[name].ToString()); 93 if (value != null) 94 pop.SetValue(model, Convert.ToDateTime(value)); 95 } 96 97 } 98 break; 99 case ModelType.Boolen: 100 bool? Value = null; 101 if (row[name] != DBNull.Value) Value = Convert.ToBoolean(row[name]); 102 pop.SetValue(model, Value); 103 break; 104 case ModelType.String: 105 { 106 pop.SetValue(model, row[name] == DBNull.Value ? null : row[name]); 107 } 108 break; 109 case ModelType.Object: 110 { 111 pop.SetValue(model, row[name] == DBNull.Value ? null : row[name]); 112 } 113 break; 114 case ModelType.Else: 115 throw new Exception("不支持该类型转换"); 116 default: 117 throw new Exception("未知类型"); 118 } 119 } 120 catch (Exception ex) 121 { 122 pop.SetValue(model,default(T)); 123 124 } 125 } 126 return model; 127 } 128 catch (Exception ex) 129 { 130 throw ex; 131 } 132 } 133 /// <summary> 134 /// 类型枚举 135 /// </summary> 136 private enum ModelType 137 { 138 //值类型 139 Struct, 140 Enum, 141 Boolen, 142 //引用类型 143 String, 144 Object, 145 Else, 146 147 148 } 149 private static ModelType GetModelType(Type modelType) 150 { 151 if (modelType.IsEnum) 152 { 153 return ModelType.Enum; 154 } 155 if (modelType == typeof(bool)) 156 { 157 return ModelType.Boolen; 158 } 159 //值类型 160 if (modelType.IsValueType) 161 { 162 return ModelType.Struct; 163 } 164 //引用类型 特殊类型处理 165 if (modelType == typeof(string)) 166 { 167 return ModelType.String; 168 } 169 //引用类型 特殊类型处理 170 return modelType == typeof(object) ? ModelType.Object : ModelType.Else; 171 } 172 #endregion
以上是关于DT转实体的主要内容,如果未能解决你的问题,请参考以下文章