54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace Face.Services.Tool
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 实体帮助
|
|||
|
|
/// </summary>
|
|||
|
|
public static class ModelHelper
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取实体属性名
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GetModelPropertyName<T>() where T : class, new()
|
|||
|
|
{
|
|||
|
|
string nameStr = "";
|
|||
|
|
|
|||
|
|
foreach (PropertyInfo pi in (new T()).GetType().GetProperties())
|
|||
|
|
{
|
|||
|
|
//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
|
|||
|
|
var name = pi.Name;
|
|||
|
|
//var value = pi.GetValue(list, null);//用pi.GetValue获得值
|
|||
|
|
//var type = value?.GetType() ?? typeof(object);//获得属性的类型
|
|||
|
|
|
|||
|
|
nameStr += "`" + name + "`,";
|
|||
|
|
}
|
|||
|
|
return nameStr.Substring(0, nameStr.Length - 1);//nameStr; 去除最后一个逗号
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static List<Dictionary<string, object>> TableToRow(DataTable tbl)
|
|||
|
|
{
|
|||
|
|
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
|
|||
|
|
foreach (DataRow Row in tbl.Rows)//循环行
|
|||
|
|
{
|
|||
|
|
Dictionary<string, object> row = new Dictionary<string, object>();
|
|||
|
|
for (int i = 0; i < Row.ItemArray.Length; i++)
|
|||
|
|
{
|
|||
|
|
row.Add(tbl.Columns[i].ColumnName, Row[i].ToString());
|
|||
|
|
}
|
|||
|
|
rows.Add(row);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return rows;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|