namespace Utility { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Configuration; using System.Data; using System.Collections; using directPacking; using System.Reflection; public class GlobalConvert { public GlobalConvert() {} public static List ConvertIListToList(IList gbList) where T : class { if (gbList != null && gbList.Count > 1) { List list = new List(); for (int i = 0; i < gbList.Count; i++) { T temp = gbList[i] as T; if (temp != null) list.Add(temp); } return list; } return null; } public static List ToList(DataTable dt) where T : class { var lst = new List(); var plist = new List(typeof(T).GetProperties()); foreach (DataRow item in dt.Rows) { T t = System.Activator.CreateInstance(); for (int i = 0; i < dt.Columns.Count; i++) { System.Reflection.PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName); if (info != null) { if (!Convert.IsDBNull(item[i])) { info.SetValue(t, item[i], null); } } } lst.Add(t); } return lst; } public void EntityToEntity(object objectsrc, object objectdest) { var sourceType = objectsrc.GetType(); var destType = objectdest.GetType(); foreach (var source in sourceType.GetProperties()) { foreach (var dest in destType.GetProperties()) { if (dest.Name == source.Name) { dest.SetValue(objectdest, source.GetValue(objectsrc)); } } } } public static T Cast(Object myobj) { Type objectType = myobj.GetType(); Type target = typeof(T); var x = Activator.CreateInstance(target, false); var z = from source in objectType.GetMembers().ToList() where source.MemberType == System.Reflection.MemberTypes.Property select source; var d = from source in target.GetMembers().ToList() where source.MemberType == System.Reflection.MemberTypes.Property select source; List members = d.Where(memberInfo => d.Select(c => c.Name) .ToList().Contains(memberInfo.Name)).ToList(); System.Reflection.PropertyInfo propertyInfo; object value; foreach (var memberInfo in members) { propertyInfo = typeof(T).GetProperty(memberInfo.Name); value = myobj.GetType().GetProperty(memberInfo.Name).GetValue(myobj, null); propertyInfo.SetValue(x, value, null); } return (T)x; } } }