using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
namespace Core.StlMes.Client.ZGMil.Common
{
class EntityTool
{
///
/// 返回实体类属性对应的数据库字段名
///
///
///
///
public static ArrayList getProperties(T t)
{
ArrayList EntilyProName = new ArrayList();
if (t == null)
{
return EntilyProName;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return EntilyProName;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
EntilyProName.Add(name);
}
return EntilyProName;
}
///
/// 数据库字段名转换成实体类属性名
///
///
///
public static string SQLColumnNameToEntityName(string strName)
{
string[] sArray = strName.Split(new char[1] { '_' });
string Rstring = "";
foreach (string str in sArray)
{
string temp = "";
//temp = str.ToLower();
temp = str.Substring(0, 1).ToUpper() + str.Substring(1).ToLower(); ;
Rstring += temp;
}
return Rstring;
}
public static Object setObjectByDataTable(Object obj, DataTable dt)
{
System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
// string value = item.GetValue(obj, null).ToString();
foreach (DataRow dr in dt.Rows)
{
if (SQLColumnNameToEntityName(dr[0].ToString()).Equals(name))
{
item.SetValue(obj, dr[1].ToString(), null);
}
}
}
return obj;
}
}
}