using JinYuan.Models.HelperAttribute;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
///
/// DataTable 映射工具
///
///
/// DataTable 映射工具
///
public static class DataTableMapper
{
///
/// 实体类型转换为 DataTable 结构(含注解解析)
///
public static DataTable ToSchema(Type type)
{
var dt = new DataTable(type.Name);
var members = type.GetMembers(BindingFlags.Public | BindingFlags.Instance);
foreach (var member in members)
{
var prop = member as PropertyInfo;
var field = member as FieldInfo;
if ((prop != null && !prop.CanRead) || (field == null && prop == null)) continue;
var attr = member.GetCustomAttribute();
var colName = attr != null ? attr.ColumnName : member.Name;
var colType = attr != null && attr.ColumnType != null ? attr.ColumnType :
(prop != null ? prop.PropertyType : field.FieldType);
var column = dt.Columns.Add(colName, colType);
column.AllowDBNull = !(colType.GetTypeInfo().IsValueType &&
!colType.GetTypeInfo().IsGenericType);
}
return dt;
}
///
/// 实体集合转换为 DataTable(含注解映射)
/// 使用: var dt = DataTableMapper.ToDataTable (employees, typeof (Employee));
///
public static DataTable ToDataTable(this IEnumerable