C#利用反射将实体类ListT转化为Datatable
生活随笔
收集整理的這篇文章主要介紹了
C#利用反射将实体类ListT转化为Datatable
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景介紹
C#中實體類的操作十分簡便,一般情況下不需要涉及ADO.NET,如果需要將實體類轉化為Datatable,這個時候就需要手動寫一個方法了,代碼如下圖所示:
public static class IEnumerableExtention{/// <summary>/// 將實體類轉化為Datatable/// </summary>/// <typeparam name="T">實體類類型</typeparam>/// <param name="_ienu">實體對象</param>/// <param name="_tableName">表名</param>/// <returns>轉化成的DataTable</returns>public static DataTable ToDataTable<T>(this IEnumerable<T> _ienu, string _tableName = "tableFromList") where T : new(){DataTable resultTable = new DataTable(_tableName);var pi = typeof(T).GetProperties(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly).ToList();if (_ienu == null || pi == null || pi.Count == 0){//當實體類為空的時候返回空的DataTable}else{pi.ForEach(p => { resultTable.Columns.Add(p.Name, p.PropertyType); });//生成Datatable列及列屬性foreach (var _ie in _ienu){var dr = resultTable.NewRow();foreach (var prop in pi){if (prop.GetMethod != null){dr.SetField(prop.Name, prop.GetValue(_ie));}}resultTable.Rows.Add(dr);}}return resultTable;}/// <summary>/// 將實體類轉化為DataSet/// </summary>/// <typeparam name="T">實體類類型</typeparam>/// <param name="_ienu"><實體對象/param>/// <param name="_tableName">表名</param>/// <returns>轉化成的DataSet</returns>public static DataSet ToDataSet<T>(this IEnumerable<T> _ienu, string _tableName = "tableFromList") where T : new(){DataSet ds = new DataSet();ds.Tables.Add(_ienu.ToDataTable(_tableName));return ds;}/// <summary>/// 將實體類的對象列表復制給另一個類型相同的實體類/// </summary>/// <typeparam name="T">源實體類對象類型</typeparam>/// <param name="_ienu">源實體類</param>/// <returns>返回的實體類</returns>public static IEnumerable<T> Copy<T>(this IEnumerable<T> _ienu) where T : new(){var resultLists = new List<T>();var pi = typeof(T).GetProperties(BindingFlags.Instance|BindingFlags.Public|BindingFlags.DeclaredOnly).ToList();if (null == _ienu || null == pi || pi.Count == 0){//源實體類對象為空則返回空實體類}else{foreach (var _ie in _ienu){var resultList = new T();foreach (var prop in pi){if (prop.SetMethod != null && prop.GetMethod != null && !prop.PropertyType.IsAbstract){prop.SetValue(resultList,prop.GetValue(_ie));}}resultLists.Add(resultList);}}return resultLists;}/// <summary>/// 將一個實體類對象列表復制給另一個類型相同的實體類,支持篩選/// </summary>/// <typeparam name="T">源實體類對象類型</typeparam>/// <param name="_ienu">源實體類</param>/// <param name="_predicate">篩選的委托</param>/// <returns></returns>public static IEnumerable<T> Copy<T>(this IEnumerable<T> _ienu, Func<T, bool> _predicate = null) where T:new(){var resultLists = new List<T>();var pi = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).ToList();if (null == _ienu || null == pi || pi.Count == 0){//源實體類對象為空則返回空實體類}else{if (_predicate != null){_ienu = _ienu.Where(_predicate);}else{foreach (var _ie in _ienu){var resultList = new T();foreach (var prop in pi){if (prop.SetMethod != null && prop.GetMethod != null && !prop.PropertyType.IsAbstract){prop.SetValue(resultList, prop.GetValue(_ie));}}resultLists.Add(resultList);}}}return resultLists;}/// <summary>/// 將一個實體類對象列表復制給另一個類型不相同的實體類,支持篩選,只復制名稱和類型都相同的屬性/// </summary>/// <typeparam name="TSource">源實體類類型</typeparam>/// <typeparam name="TResult">目標實體類類型</typeparam>/// <param name="_ienu">源實體類</param>/// <param name="_predicate">篩選的委托</param>/// <returns>返回的實體類</returns>public static List<TResult> CopyTo<TSource, TResult>(this IEnumerable<TSource> _ienu, Func<TSource, bool> _predicate = null) where TResult : new(){var resultLists = new List<TResult>();if (_ienu == null){throw new ArgumentNullException(nameof(_ienu));}var piSource = typeof(TSource).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).ToList();//獲取來源實體類屬性var piResult = typeof(TResult).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).ToList();//獲取返回實體類屬性if (piSource == null || piSource.Count == 0 || piResult == null || piResult.Count == 0){//返回空實體類return resultLists;}if (null != _predicate){_ienu = _ienu.Where(_predicate);}if (_ienu.Count() > 0)//判斷來源實體類中列表數量{List<Tuple<PropertyInfo, bool>> PropLists = new List<Tuple<PropertyInfo, bool>>();piResult.ForEach(p =>{if (piSource.Exists(q => q.Name.Equals(p.Name))){PropLists.Add(Tuple.Create(p, p.PropertyType.Equals(piResult.FirstOrDefault(q => q.Name.Equals(p.Name)).PropertyType)));}});foreach (var _ie in _ienu){var resultList = new TResult();var samePropLists = PropLists.Where(p => p.Item2 == true).ToList();//提取類型也相同的屬性foreach (var prop in samePropLists){if (prop.Item1.GetMethod != null && _ie.GetType().GetProperty(prop.Item1.Name).SetMethod != null){var value = piSource.FirstOrDefault(p => p.Name.Equals(prop.Item1.Name)).GetValue(_ie);prop.Item1.SetValue(resultList, value);}}resultLists.Add(resultList);}}return resultLists;}}IEnumerableExtention類中包含五個方法:第一個直接將實體類轉化為Datatable,第二個是在第一個的基礎上包了一層DataSet并返回。第三個和第四個是將實體類列表復制給類型相同的另一個實體類,第四個支持篩選。第五個是將實體類復制給類型不相同的實體類,但是只支持復制類型和名字都相同的屬性。
總結
以上是生活随笔為你收集整理的C#利用反射将实体类ListT转化为Datatable的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符串匹配之KMP---全力解析
- 下一篇: 操作系统的SPOOLING技术