反射datareader填充数据

public class Class1<T>
    {
        public IList<T> GetData(SqlDataReader reader)
        {
            IList<T> list = new List<T>();
            Type type = typeof(T);
            PropertyInfo[] properties = type.GetProperties();

            while (reader.Read())
            {
                T t = Activator.CreateInstance<T>();
                for (int i = 0; i < properties.Length; i++)
                {
                    properties[i].SetValue(t, reader[i + 1], null);

                }

                list.Add(t);
            }

            return list;
        }
    }

你可能感兴趣的:(反射datareader填充数据)