| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- /*******************************************************************************
- * 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
- {
- /// <summary>
- /// 系统各种数据类型之间转换
- /// </summary>
- static public class DataConvert
- {
- #region ToDataTable()
- /// <summary>
- /// 将控件的数据源转成DataTable类型
- /// </summary>
- /// <param name="source">数据源</param>
- /// <param name="member">绑定的列成员</param>
- /// <returns>转换后的DataTable数据源</returns>
- 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<T>()
- public static object[] ToObjectArray<T>(T[] array)
- {
- if (array == null)
- {
- return null;
- }
- List<object> list = new List<object>();
- foreach (object obj in array)
- {
- list.Add(obj);
- }
- return list.ToArray();
- }
- public static T[] ToTArray<T>(object[] array)
- {
- if (array == null)
- {
- return null;
- }
- List<T> list = new List<T>();
- foreach (T obj in array)
- {
- list.Add(obj);
- }
- return list.ToArray();
- }
- #endregion
- /// <summary>
- /// 把Table转换成List
- /// </summary>
- /// <typeparam name="T">指定转换的类型</typeparam>
- /// <param name="pDt">DataTable</param>
- /// <returns></returns>
- public static List<T> TableConvertToObject<T>(DataTable pDt)
- {
- if (pDt == null)
- {
- return default(List<T>);
- }
- List<T> lst = new List<T>();
- for (int i = 0; i < pDt.Rows.Count; i++)
- {
- lst.Add(DataRowConvertToObject<T>(pDt.Rows[i]));
- }
- return lst;
- }
- /// <summary>
- /// 把DataRow转换成实体类Model
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="row">DataRow</param>
- /// <returns></returns>
- public static T DataRowConvertToObject<T>(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;
- }
- /// <summary>
- /// 把a的所有属性值,赋值给b的对应属性。
- /// </summary>
- /// <param name="a">对象a。</param>
- /// <param name="b">对象b。</param>
- public static void Convert(object a, object b)
- {
- List<Type> listType = new List<Type> {
- 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<string, string>),
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 把a的所有属性值,赋值给b的对应属性。
- /// 除了Guid类型外
- /// </summary>
- /// <param name="a">对象a。</param>
- /// <param name="b">对象b。</param>
- public static void ConvertWithoutGuid(object a, object b)
- {
- List<Type> listType = new List<Type> {
- 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<string, string>)
- };
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 把a的所有属性值,赋值给b的对应属性。
- /// </summary>
- /// <param name="a">对象a。</param>
- /// <param name="b">对象b。</param>
- public static void ConvertWithoutTimeStamp(object a, object b)
- {
- List<Type> listType = new List<Type> {
- 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<string, string>)
- };
- 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);
- }
- }
- }
- }
- /// <summary>
- /// 复制除某个字段以外的所有字段
- /// </summary>
- /// <param name="a">对象a。</param>
- /// <param name="b">对象b。</param>
- /// <param name="key">字段</param>
- public static void ConvertWithoutKey(object a, object b, string key)
- {
- List<Type> listType = new List<Type> {
- 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<string, string>)
- };
- 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 返回类型格式
- /// <summary>
- /// 返回类型格式
- /// </summary>
- /// <param name="obj">字段值</param>
- /// <param name="type">字段类型</param>
- /// <returns></returns>
- 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
- /// <summary>
- /// 将传入的List 转换为 SQL 中的IN条件 例如 1,2,3,4
- /// </summary>
- /// <param name="pListKey">List数据</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 将类集合转换为DataTable
- /// </summary>
- /// <typeparam name="T">类</typeparam>
- /// <param name="objects">类集合</param>
- /// <returns></returns>
- public static DataTable ObjectConvertToTable<T>(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";
- /// <summary>
- /// 10进制转换成36进制
- /// </summary>
- /// <param name="val"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 36进制转换成10进制
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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
- }
- }
|