/******************************************************************************* * Copyright(c) 2012 dongke All rights reserved. / Confidential * 类的信息: * 1.程序名称:Convert.cs * 2.功能描述:系统各种数据类型之间转换 * 编辑履历: * 作者 日期 版本 修改内容 * 欧阳涛 2012/07/04 1.00 新建 *******************************************************************************/ using System; using System.Collections.Generic; using System.Data; using System.Reflection; using System.Windows.Forms; namespace Dongke.IBOSS.PRD.Basics.Library { /// /// 系统各种数据类型之间转换 /// static public class DataConvert { #region ToDataTable() /// /// 将控件的数据源转成DataTable类型 /// /// 数据源 /// 绑定的列成员 /// 转换后的DataTable数据源 public static DataTable ToDataTable(object source, string member) { BindingSource bindingSource = source as System.Windows.Forms.BindingSource; if (bindingSource != null) { if (string.IsNullOrEmpty(bindingSource.DataMember) == false) { member = bindingSource.DataMember; } source = bindingSource.DataSource; while (true) { if (source is System.Windows.Forms.BindingSource) { source = ((System.Windows.Forms.BindingSource)source).DataSource; if (source is DataSet) { return ((DataSet)source).Relations[member].ChildTable; } } else { break; } } } if (source is DataSet) { DataSet set = (DataSet)source; DataTable table = set.Tables[member]; if (table != null) { return table; } string[] splits = member.Split('.'); if (splits.Length == 2) { table = set.Tables[splits[0]]; DataRelation relation = set.Relations[splits[1]]; if (relation.ParentTable == table) { return relation.ChildTable; } } return null; } else if (source is DataTable) { return (DataTable)source; } else if (source is DataView) { return ((DataView)source).Table; } else { return null; } } #endregion #region ToObjectArray() public static object[] ToObjectArray(T[] array) { if (array == null) { return null; } List list = new List(); foreach (object obj in array) { list.Add(obj); } return list.ToArray(); } public static T[] ToTArray(object[] array) { if (array == null) { return null; } List list = new List(); foreach (T obj in array) { list.Add(obj); } return list.ToArray(); } #endregion /// /// 把Table转换成List /// /// 指定转换的类型 /// DataTable /// public static List TableConvertToObject(DataTable pDt) { if (pDt == null) { return default(List); } List lst = new List(); for (int i = 0; i < pDt.Rows.Count; i++) { lst.Add(DataRowConvertToObject(pDt.Rows[i])); } return lst; } /// /// 把DataRow转换成实体类Model /// /// 类型 /// DataRow /// public static T DataRowConvertToObject(DataRow row) { Type t = typeof(T); object obj = Activator.CreateInstance(t); PropertyInfo[] property_infos = t.GetProperties(); if (row != null) { foreach (PropertyInfo property_info in property_infos) { string property_name = property_info.Name; try { if (row[property_name] != null) { string fieldType = property_info.PropertyType.FullName; property_info.SetValue(obj, GetType(row[property_name], fieldType), null); } } catch { } } } return (T)obj; } /// /// 把a的所有属性值,赋值给b的对应属性。 /// /// 对象a。 /// 对象b。 public static void Convert(object a, object b) { List listType = new List { typeof(Boolean), typeof(Boolean?), typeof(Int64), typeof(Int64?), typeof(Int32), typeof(Int32?), typeof(String), typeof(Double), typeof(Double?), typeof(DateTime), typeof(DateTime?), typeof(Byte[]), typeof(Guid), typeof(Guid?), typeof(Decimal), typeof(Decimal?), typeof(Char?), typeof(Dictionary), typeof(DataTable), typeof(DataSet) }; if (a == null || b == null) { return; } //循环所有属性,从a中取值并赋给b的对应属性。 foreach (var item in a.GetType().GetProperties()) { if (listType.Contains(item.PropertyType)) { object value = item.GetValue(a, null); var property = b.GetType().GetProperty(item.Name); if (property != null) { property.SetValue(b, value, null); } } } } /// /// 把a的所有属性值,赋值给b的对应属性。 /// 除了Guid类型外 /// /// 对象a。 /// 对象b。 public static void ConvertWithoutGuid(object a, object b) { List listType = new List { typeof(Boolean), typeof(Boolean?), typeof(Int32), typeof(Int32?), typeof(String), typeof(Double), typeof(Double?), typeof(DateTime), typeof(DateTime?), typeof(Byte[]), typeof(Decimal), typeof(Decimal?), //typeof(Guid), typeof(Dictionary) }; if (a == null || b == null) { return; } //循环所有属性,从a中取值并赋给b的对应属性。 foreach (var item in a.GetType().GetProperties()) { if (listType.Contains(item.PropertyType)) { object value = item.GetValue(a, null); var property = b.GetType().GetProperty(item.Name); if (property != null) { property.SetValue(b, value, null); } } } } /// /// 把a的所有属性值,赋值给b的对应属性。 /// /// 对象a。 /// 对象b。 public static void ConvertWithoutTimeStamp(object a, object b) { List listType = new List { typeof(Boolean), typeof(Boolean?), typeof(Int32), typeof(Int32?), typeof(String), typeof(Double), typeof(Double?), typeof(DateTime), typeof(DateTime?), typeof(Byte[]), typeof(Guid), typeof(Guid?), typeof(Decimal), typeof(Decimal?), typeof(Dictionary) }; if (a == null || b == null) { return; } //循环所有属性,从a中取值并赋给b的对应属性。 foreach (var item in a.GetType().GetProperties()) { if (item.Name.CompareTo("timestamp") == 0) { continue; } if (listType.Contains(item.PropertyType)) { object value = item.GetValue(a, null); var property = b.GetType().GetProperty(item.Name); if (property != null) { property.SetValue(b, value, null); } } } } /// /// 复制除某个字段以外的所有字段 /// /// 对象a。 /// 对象b。 /// 字段 public static void ConvertWithoutKey(object a, object b, string key) { List listType = new List { typeof(Boolean), typeof(Boolean?), typeof(Int32), typeof(Int32?), typeof(String), typeof(Double), typeof(Double?), typeof(DateTime), typeof(DateTime?), typeof(Byte[]), typeof(Guid), typeof(Guid?), typeof(Decimal), typeof(Decimal?), typeof(Dictionary) }; if (a == null || b == null) { return; } //循环所有属性,从a中取值并赋给b的对应属性。 foreach (var item in a.GetType().GetProperties()) { if (item.Name.CompareTo(key) == 0) { continue; } if (listType.Contains(item.PropertyType)) { object value = item.GetValue(a, null); var property = b.GetType().GetProperty(item.Name); if (property != null) { property.SetValue(b, value, null); } } } } #region 返回类型格式 /// /// 返回类型格式 /// /// 字段值 /// 字段类型 /// private static dynamic GetType(object obj, string type) { if (obj == System.DBNull.Value) { return null; } if (type.ToLower().Contains("string")) { return obj.ToString() == "" ? null : obj.ToString(); } else if (type.ToLower().Contains("int")) { return int.Parse(obj.ToString()); } else if (type.ToLower().Contains("datetime")) { return DateTime.Parse(obj.ToString()); } else if (type.ToLower().Contains("decimal")) { return decimal.Parse(obj.ToString()); } else if (type.ToLower().Contains("double")) { return double.Parse(obj.ToString()); } else { return obj; } } #endregion /// /// 将传入的List 转换为 SQL 中的IN条件 例如 1,2,3,4 /// /// List数据 /// public static string ConvertListToSqlInWhere(int[] pListKey) { System.Text.StringBuilder sbWhere = new System.Text.StringBuilder(); sbWhere.Append("("); foreach (int itemKey in pListKey) { sbWhere.Append(itemKey + ","); } string strWhere = sbWhere.ToString(); if (strWhere.Length >= 1) { strWhere = strWhere.Substring(0, strWhere.Length - 1); } if (strWhere.Length > 0) { strWhere = strWhere + ")"; } else { strWhere = string.Empty; } return strWhere; } /// /// 将类集合转换为DataTable /// /// /// 类集合 /// public static DataTable ObjectConvertToTable(T[] objects) { if (objects == null || objects.Length <= 0) { return new DataTable(); } DataTable dTable = new DataTable("Table1"); Type t = typeof(T); PropertyInfo[] property_infos = t.GetProperties(); foreach (PropertyInfo property_info in property_infos) { string property_name = property_info.Name; dTable.Columns.Add(property_name); } foreach (T itemT in objects) { DataRow newRowObject = dTable.NewRow(); foreach (PropertyInfo property_info in property_infos) { string property_name = property_info.Name; var property_value = property_info.GetValue(itemT, null); newRowObject[property_name] = property_value; } dTable.Rows.Add(newRowObject); } return dTable; } #region 10 36 转换 private const string X36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /// /// 10进制转换成36进制 /// /// /// public static string Convert10To36(long val, int length = 8) { string result = ""; while (val >= 36) { int valtmp = (int)(val % 36); //result = (valtmp >= 0 && valtmp <= 9 ? (char)(valtmp + 48) : (char)(valtmp + 55)) + result; result = X36[valtmp] + result; val /= 36; } if (val >= 0) { //result = (val >= 0 && val <= 9 ? (char)(val + 48) : (char)(val + 55)) + result; result = X36[(int)val] + result; } if (result.Length < length) { result = result.PadLeft(length, '0'); } else if (result.Length > length) { result = result.Substring(result.Length - length); } return result; } /// /// 36进制转换成10进制 /// /// /// public static long Convert36To10(string str) { long result = 0; int len = str.Length; for (int i = len; i > 0; i--) { result += X36.IndexOf(str[i - 1]) * System.Convert.ToInt64(Math.Pow(36, len - i)); } return result; } #endregion } }