日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

可能是多余的

發布時間:2025/3/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 可能是多余的 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在做項目的過程中發現有一種情況,就是實體類是不確定的,沒辦法寫出對應的實體類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

總結

以上是生活随笔為你收集整理的可能是多余的的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。