using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Web.Script.Serialization;
namespace HH.WMS.Utils
{
public partial class ZConvert
{
///
/// 转换 DataTable 对象为 IList 对象
///
/// 数据集合
/// 数组对象
public static T[] ToArray(DataTable datas) where T : class, new()
{
List list = ToList(datas) as List;
return list.ToArray();
}
///
/// 转换IList对象为DataTable对象
///
/// 数据集合
/// DataTable对象
public static DataTable ToDataTable(IList datas)
{
return ToDataTable(datas, null);
}
///
/// 转换IList对象为DataTable对象
///
/// 数据集合
/// DataTable对象
public static DataTable ToDataTable(T[] datas)
{
return ToDataTable(datas, null);
}
///
/// 转换IList对象为DataTable对象
///
/// 数据集合
/// 要创建的表名
/// DataTable对象
public static DataTable ToDataTable(IList datas, string tableName)
{
Type type = typeof(T);
if (string.IsNullOrEmpty(tableName))
{
tableName = type.Name;
}
DataTable table = new DataTable(tableName);
table.BeginLoadData();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo info in properties)
{
string typeName = info.PropertyType.ToString();
if (info.PropertyType.IsGenericType)
{
typeName = info.PropertyType.GetGenericArguments()[0].ToString();
}
Type type2 = Type.GetType(typeName, false);
if (type2 != null)
{
table.Columns.Add(info.Name, type2);
}
}
if ((datas != null) && (datas.Count > 0))
{
foreach (object obj2 in datas)
{
DataRow row = table.NewRow();
foreach (PropertyInfo info2 in properties)
{
if ((Type.GetType(info2.PropertyType.ToString(), false) != null) && (info2.GetValue(obj2, null) != null))
{
row[info2.Name] = info2.GetValue(obj2, null);
}
}
table.Rows.Add(row);
}
}
table.EndLoadData();
table.AcceptChanges();
return table;
}
public static DataTable ListToDataTable(object datas, string tableName)
{
Type type = ZGeneric.GetGenericType(datas);
if (string.IsNullOrEmpty(tableName))
tableName = type.Name;
DataTable table = new DataTable(tableName);
table.BeginLoadData();
var properties = ZReflection.GetProperties(type);
foreach (var p in properties)
{
Type colType = p.Value.PropertyType;
string typeName = colType.ToString();
if (colType.IsGenericType)
typeName = colType.GetGenericArguments()[0].ToString();
Type newType = Type.GetType(typeName, false);
if (newType != null)
table.Columns.Add(p.Value.Name, newType);
}
IEnumerator enumerator = ((dynamic)datas).GetEnumerator();
while (enumerator.MoveNext())
{
DataRow row = table.NewRow();
foreach (var p in properties)
{
var value = ZGeneric.GetValue(enumerator.Current, p.Value.Name);
if ((Type.GetType(p.Value.PropertyType.ToString(), false) != null) && (value != null))
row[p.Value.Name] = value;
}
table.Rows.Add(row);
}
table.EndLoadData();
table.AcceptChanges();
return table;
}
///
/// 转换IList对象为DataTable对象
///
/// 数据集合
/// 要创建的表名
/// DataTable对象
public static DataTable ToDataTable(T[] datas, string tableName)
{
IList list;
if ((datas == null) || (datas.Length == 0))
{
list = new List();
}
else
{
list = new List(datas);
}
return ToDataTable(list, tableName);
}
///
/// 转换 DataTable 对象为 IList 对象
///
/// 数据集合
/// IList 对象
public static IList ToList(DataTable datas) where T : class, new()
{
IList list = new List();
if ((datas != null) && (datas.Rows.Count != 0))
{
PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (DataRow row in datas.Rows)
{
T local = Activator.CreateInstance();
foreach (DataColumn column in datas.Columns)
{
object obj2 = null;
if (row.RowState == DataRowState.Deleted)
{
obj2 = row[column, DataRowVersion.Original];
}
else
{
obj2 = row[column];
}
if (obj2 != DBNull.Value)
{
foreach (PropertyInfo info in properties)
{
if (column.ColumnName.Equals(info.Name, StringComparison.CurrentCultureIgnoreCase))
{
info.SetValue(local, obj2, null);
}
}
}
}
list.Add(local);
}
}
return list;
}
///
/// Json 字符串 转换为 DataTable数据集合
///
///
///
public static DataTable ToDataTableTwo(string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize(json);
if (arrayList.Count > 0)
{
foreach (Dictionary dictionary in arrayList)
{
if (dictionary.Keys.Count() == 0)
{
result = dataTable;
return result;
}
//Columns
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current]!=null?dictionary[current].GetType():typeof(string));
}
}
//Rows
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result = dataTable;
return result;
}
}
}