可能是多余的
在做項目的過程中發現有一種情況,就是實體類是不確定的,沒辦法寫出對應的實體類cs文件,那么我們怎么來在數據傳輸過程中形成這么個動態的類
1,首先將從數據庫查出的dataset或datatable轉換成?List<Dictionary<string, string>>
/// <summary>/// 將DataSet轉換成List集合/// </summary>/// <param name="dataSet"></param>/// <returns></returns>public static List<Dictionary<string, string>> ConvertToIEnumerable(DataSet dataSet){List<Dictionary<string, string>> list = null;DataTable dt = dataSet.Tables[0];if (dt.Rows.Count > 0){list = new List<Dictionary<string, string>>();foreach (DataRow dr in dt.Rows){Dictionary<string, string> dict = new Dictionary<string, string>();foreach (DataColumn dc in dt.Columns){dict.Add(dc.ColumnName, dr[dc.ColumnName].ToString());}list.Add(dict);}}return list;}2.然后將List<Dictionary<string, string>> 轉換成 IEnumerable<IDictionary>
public IEnumerable<IDictionary> GetEnumerable(List<Dictionary<string, string>> SourceList){for (int i = 0; i < SourceList.Count; i++){var dict = new Dictionary<string, string>();dict = SourceList[i];yield return dict;}}3,將?IEnumerable<IDictionary> 轉換成?List<object> 這時需要用到一個?DataSourceCreator 類
public static class DataSourceCreator{private static readonly Regex PropertNameRegex =new Regex(@"^[A-Za-z]+[A-Za-z1-9_]*$", RegexOptions.Singleline);public static List<object> ToDataSource(this IEnumerable<IDictionary> list){IDictionary firstDict = null;bool hasData = false;foreach (IDictionary currentDict in list){hasData = true;firstDict = currentDict;break;}if (!hasData){return new List<object> { };}if (firstDict == null){throw new ArgumentException("IDictionary entry cannot be null");}Type objectType = null;TypeBuilder tb = GetTypeBuilder(list.GetHashCode());ConstructorBuilder constructor =tb.DefineDefaultConstructor(MethodAttributes.Public |MethodAttributes.SpecialName |MethodAttributes.RTSpecialName);foreach (DictionaryEntry pair in firstDict){if (PropertNameRegex.IsMatch(Convert.ToString(pair.Key), 0)){CreateProperty(tb,Convert.ToString(pair.Key),pair.Value == null ?typeof(object) :pair.Value.GetType());}else{throw new ArgumentException(@"Each key of IDictionary must bealphanumeric and start with character.");}}objectType = tb.CreateType();return GenerateArray(objectType, list, firstDict);}private static List<object> GenerateArray(Type objectType, IEnumerable<IDictionary> list, IDictionary firstDict){var itemsSource = new List<object>();foreach (var currentDict in list){if (currentDict == null){throw new ArgumentException("IDictionary entry cannot be null");}object row = Activator.CreateInstance(objectType);foreach (DictionaryEntry pair in firstDict){if (currentDict.Contains(pair.Key)){PropertyInfo property =objectType.GetProperty(Convert.ToString(pair.Key));property.SetValue(row,Convert.ChangeType(currentDict[pair.Key],property.PropertyType,null),null);}}itemsSource.Add(row);}return itemsSource;}private static TypeBuilder GetTypeBuilder(int code){AssemblyName an = new AssemblyName("TempAssembly" + code);AssemblyBuilder assemblyBuilder =AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");TypeBuilder tb = moduleBuilder.DefineType("TempType" + code, TypeAttributes.Public |TypeAttributes.Class |TypeAttributes.AutoClass |TypeAttributes.AnsiClass |TypeAttributes.BeforeFieldInit |TypeAttributes.AutoLayout, typeof(object));return tb;}private static void CreateProperty(TypeBuilder tb, string propertyName, Type propertyType){FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName,propertyType,FieldAttributes.Private);PropertyBuilder propertyBuilder =tb.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);MethodBuilder getPropMthdBldr =tb.DefineMethod("get_" + propertyName,MethodAttributes.Public |MethodAttributes.SpecialName |MethodAttributes.HideBySig,propertyType, Type.EmptyTypes);ILGenerator getIL = getPropMthdBldr.GetILGenerator();getIL.Emit(OpCodes.Ldarg_0);getIL.Emit(OpCodes.Ldfld, fieldBuilder);getIL.Emit(OpCodes.Ret);MethodBuilder setPropMthdBldr =tb.DefineMethod("set_" + propertyName,MethodAttributes.Public |MethodAttributes.SpecialName |MethodAttributes.HideBySig,null, new Type[] { propertyType });ILGenerator setIL = setPropMthdBldr.GetILGenerator();setIL.Emit(OpCodes.Ldarg_0);setIL.Emit(OpCodes.Ldarg_1);setIL.Emit(OpCodes.Stfld, fieldBuilder);setIL.Emit(OpCodes.Ret);propertyBuilder.SetGetMethod(getPropMthdBldr);propertyBuilder.SetSetMethod(setPropMthdBldr);}}4,描述整個過程的代碼
DataSet dsResult = (查出來的表結果); List<Dictionary<string, string>> dicList= ConvertToIEnumerable(dsResult);List<object> list= GetEnumerable(dicList).ToDataSource(); //這樣這個object就是個不用在項目中寫cs文件定義的實體類 ********************** //想看具體屬性的值就反射回來 foreach (object obj in list) {Dictionary<string, string> result = new Dictionary<string, string>();System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties();foreach (System.Reflection.PropertyInfo item in properties)//獲取該鍵值{string name = item.Name;string value = item.GetValue(model, null).ToString();result.Add(name, value);} }?
轉載于:https://www.cnblogs.com/lnice/p/7700591.html
總結
- 上一篇: 二、python基础
- 下一篇: [PY3]——IO——文件目录操作