| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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
- {
- /// <summary>
- /// 返回实体类属性对应的数据库字段名
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="t"></param>
- /// <returns></returns>
- public static ArrayList getProperties<T>(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;
- }
- /// <summary>
- /// 数据库字段名转换成实体类属性名
- /// </summary>
- /// <param name="strName"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|