You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
6.8 KiB
C#
186 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Reflection;
|
|
using System.Data;
|
|
|
|
namespace Estsh.Web.Util
|
|
{
|
|
public static class ModelConvert
|
|
{
|
|
/// <summary>
|
|
/// 根据模板获得该类可读写的字段名数组
|
|
/// </summary>
|
|
/// <typeparam name="T">模版类</typeparam>
|
|
/// <returns>字段名数组</returns>
|
|
public static string[] GetFieldNames<T>()
|
|
{
|
|
List<string> fieldList = new List<string>();
|
|
Type type = typeof(T);
|
|
PropertyInfo[] propertys = type.GetProperties();
|
|
for (int i = 0; i < propertys.Length; i++)
|
|
{
|
|
// 只包含可读写的字段
|
|
if (propertys[i].CanRead && propertys[i].CanWrite)
|
|
{
|
|
fieldList.Add(propertys[i].Name);
|
|
}
|
|
}
|
|
return fieldList.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据模板类获得实体的字段键值对
|
|
/// </summary>
|
|
/// <typeparam name="T">模版类</typeparam>
|
|
/// <param name="model">实体</param>
|
|
/// <returns>字段键值对</returns>
|
|
public static Dictionary<string, object> GetFieldNameValuePairs<T>(T model)
|
|
{
|
|
Dictionary<string, object> valueDict = new Dictionary<string, object>();
|
|
Type type = typeof(T);
|
|
PropertyInfo[] propertys = type.GetProperties();
|
|
for (int i = 0; i < propertys.Length; i++)
|
|
{
|
|
// 只包含可读写的字段
|
|
if (propertys[i].CanRead && propertys[i].CanWrite)
|
|
{
|
|
object value = propertys[i].GetValue(model, null);
|
|
valueDict.Add(propertys[i].Name, value);
|
|
}
|
|
}
|
|
return valueDict;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据实体的属性构造一张空表
|
|
/// </summary>
|
|
/// <typeparam name="T">模版类</typeparam>
|
|
/// <returns></returns>
|
|
public static DataTable BuildSchema<T>()
|
|
{
|
|
DataTable tbSchema = new DataTable();
|
|
Type type = typeof(T);
|
|
foreach (PropertyInfo property in type.GetProperties())
|
|
{
|
|
tbSchema.Columns.Add(property.Name, property.PropertyType);
|
|
}
|
|
return tbSchema;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实体数组到数据表的赋值
|
|
/// </summary>
|
|
/// <typeparam name="T">模版类</typeparam>
|
|
/// <param name="modelArray"></param>
|
|
/// <returns></returns>
|
|
public static DataTable ToDataTable<T>(T[] modelArray)
|
|
{
|
|
DataTable dt = BuildSchema<T>();
|
|
for (int i = 0; i < modelArray.Length; i++)
|
|
{
|
|
DataRow row = ToDataRow<T>(modelArray[i], dt);
|
|
dt.Rows.Add(row);
|
|
}
|
|
return dt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实体到数据行的同名字段同步
|
|
/// </summary>
|
|
/// <typeparam name="T">模版类</typeparam>
|
|
/// <param name="model"></param>
|
|
/// <param name="dt"></param>
|
|
/// <returns></returns>
|
|
public static DataRow ToDataRow<T>(T model, DataTable dt)
|
|
{
|
|
DataRow row = dt.NewRow();
|
|
Type type = typeof(T);
|
|
foreach (PropertyInfo property in type.GetProperties())
|
|
{
|
|
if (row.Table.Columns.Contains(property.Name))
|
|
{
|
|
row[property.Name] = property.GetValue(model, null);
|
|
}
|
|
}
|
|
return row;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据表到实体数组的赋值
|
|
/// </summary>
|
|
/// <typeparam name="T">模版类</typeparam>
|
|
/// <param name="dt"></param>
|
|
/// <returns></returns>
|
|
public static T[] ToModelArray<T>(DataTable dt)
|
|
{
|
|
T[] modelArray = new T[dt.Rows.Count];
|
|
for (int i = 0; i < dt.Rows.Count; i++)
|
|
{
|
|
modelArray[i] = ToModel<T>(dt.Rows[i]);
|
|
}
|
|
return modelArray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据行到实体的同名字段同步
|
|
/// </summary>
|
|
/// <typeparam name="T">模版类</typeparam>
|
|
/// <param name="row"></param>
|
|
/// <param name="model"></param>
|
|
public static T ToModel<T>(DataRow row)
|
|
{
|
|
Type type = typeof(T);
|
|
T model = (T)Activator.CreateInstance(type);
|
|
foreach (PropertyInfo property in type.GetProperties())
|
|
{
|
|
if (row.Table.Columns.Contains(property.Name) && property.CanWrite && row[property.Name] != null && row[property.Name] != DBNull.Value)
|
|
{
|
|
object value = null;
|
|
switch (Type.GetTypeCode(property.PropertyType))
|
|
{
|
|
case TypeCode.Boolean:
|
|
value = Convert.ToBoolean(row[property.Name]);
|
|
break;
|
|
case TypeCode.DateTime:
|
|
value = Convert.ToDateTime(row[property.Name]);
|
|
break;
|
|
case TypeCode.Decimal:
|
|
value = Convert.ToDecimal(row[property.Name]);
|
|
break;
|
|
case TypeCode.Single:
|
|
value = Convert.ToSingle(row[property.Name]);
|
|
break;
|
|
case TypeCode.Double:
|
|
value = Convert.ToDouble(row[property.Name]);
|
|
break;
|
|
case TypeCode.Int16:
|
|
value = Convert.ToInt16(row[property.Name]);
|
|
break;
|
|
case TypeCode.Int32:
|
|
value = Convert.ToInt32(row[property.Name]);
|
|
break;
|
|
case TypeCode.Int64:
|
|
value = Convert.ToInt64(row[property.Name]);
|
|
break;
|
|
case TypeCode.SByte:
|
|
value = Convert.ToSByte(row[property.Name]);
|
|
break;
|
|
case TypeCode.Byte:
|
|
value = Convert.ToByte(row[property.Name]);
|
|
break;
|
|
case TypeCode.String:
|
|
value = Convert.ToString(row[property.Name]);
|
|
break;
|
|
default:
|
|
value = row[property.Name];
|
|
break;
|
|
}
|
|
property.SetValue(model, value, null);
|
|
}
|
|
}
|
|
return model;
|
|
}
|
|
}
|
|
}
|