首页 > 代码库 > 返回集合

返回集合

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Data;using System.Reflection;using System.Collections;using System.Data.Common;using System.Diagnostics;namespace ceDemo{    class Program    {        static void Main(string[] args)        {            List<csDemp> mdList = sp.GetDataInstanceList<csDemp>();        }    }    public static class PropertyExtensions    {        public static void FastSetValue(this PropertyInfo propertyInfo, object obj, object value)        {            if (propertyInfo == null)            {                throw new ArgumentNullException("propertyInfo");            }            GetterSetterFactory.GetPropertySetterWrapper(propertyInfo).Set(obj, value);        }    }    public static class GetterSetterFactory    {        private static readonly Hashtable s_setterDict = Hashtable.Synchronized(new Hashtable(1024));        internal interface ISetValue        {            void Set(object target, object val);        }        internal static ISetValue GetPropertySetterWrapper(PropertyInfo propertyInfo)        {            var property = (ISetValue)s_setterDict[propertyInfo];            if (property == null)            {                property = CreatePropertySetterWrapper(propertyInfo);                s_setterDict[propertyInfo] = property;            }            return property;        }        internal static ISetValue CreatePropertySetterWrapper(PropertyInfo propertyInfo)        {            if (propertyInfo == null)            {                throw new ArgumentNullException("propertyInfo");            }            if (propertyInfo.CanWrite == false)            {                throw new NotSupportedException("属性不支持写操作。");            }            var mi = propertyInfo.GetSetMethod(true);            if (mi.GetParameters().Length > 1)            {                throw new NotSupportedException("不支持构造索引器属性的委托。");            }            var instanceType = typeof(SetterWrapper<,>).MakeGenericType(propertyInfo.DeclaringType, propertyInfo.PropertyType);            return (ISetValue)Activator.CreateInstance(instanceType, propertyInfo);        }        public static void Clear()        {            s_setterDict.Clear();        }    }    public class SetterWrapper<TTarget, TValue> : GetterSetterFactory.ISetValue    {        private Action<TTarget, TValue> _setter;        public SetterWrapper(PropertyInfo propertyInfo)        {            if (propertyInfo == null)            {                throw new ArgumentNullException("propertyInfo");            }            if (propertyInfo.CanWrite == false)            {                throw new NotSupportedException("属性不支持写操作。");            }            var m = propertyInfo.GetSetMethod(true);            _setter = (Action<TTarget, TValue>)Delegate.CreateDelegate(typeof(Action<TTarget, TValue>), null, m);        }        public void SetValue(TTarget target, TValue val)        {            _setter(target, val);        }        public void Set(object target, object val)        {            _setter((TTarget)target, (TValue)val);        }    }    public class sp    {        /// <summary>        /// 获取实体列表        /// </summary>        /// <typeparam name="T">类型</typeparam>        /// <param name="functionName">方法名</param>        /// <param name="paramList">参数列表</param>        /// <returns>实体列表</returns>        public static List<T> GetDataInstanceList<T>()        {            List<T> lists = new List<T>();            DataTable dt = new DataTable();            dt.Columns.Add(new DataColumn("id"));            dt.Columns.Add(new DataColumn("Name"));            DataRow row0 = dt.NewRow();            row0[0] = "1";            row0[1] = "张三";            dt.Rows.Add(row0);            DataRow row3 = dt.NewRow();            row3[0] = "2";            row3[1] = "李四";            dt.Rows.Add(row3);            DataRow row5 = dt.NewRow();            row5[0] = "3";            row5[1] = "王五";            dt.Rows.Add(row5);            var dataType = typeof(T);            var infos = dataType.GetProperties(BindingFlags.Instance | BindingFlags.Public);            foreach (DataRow row in dt.Rows.AsParallel())            {                var obj = (T)Activator.CreateInstance(dataType);                foreach (DataColumn col in dt.Columns.AsParallel())                {                    try                    {                        foreach (PropertyInfo info in infos)                        {                            if (info.Name.ToLower() == col.ColumnName.ToLower())                            {                                if (row[col].GetType().FullName != "System.DBNull")                                {                                    info.FastSetValue(obj, row[col]);                                }                            }                        }                    }                    catch                    {                    }                }                lists.Add(obj);            }            GetterSetterFactory.Clear();            return lists;        }        public static T GetCopy<T>()        {            var obj = typeof(T);            var tar_obj = (T)Activator.CreateInstance(obj);            Type typeinstance = obj.GetType();            foreach (PropertyInfo property in typeinstance.GetProperties())            {                property.SetValue(tar_obj, property.GetValue(obj, null), null);            }            return tar_obj;        }    }    public class csDemp    {        public string id { get; set; }        public string name { get; set; }    }}

 

返回集合