protected object AssembleObject(Type type, System.Data.IDataReader reader)
{ object o = Activator.CreateInstance(type); if (o == null) return null; PropertyInfo[] properties = type.GetProperties();//声明一个获取类型属性的变量 for (int i = 0; i < reader.FieldCount; i++) { PropertyInfo property = type.GetProperty(reader.GetName(i));//得到第i列的所有属性 object val = reader[i];//第i列 if (val != DBNull.Value && property != null && property.CanWrite)//判断对象是否为空,属性是否为空,属性是否可写!如果都为true property.SetValue(o, reader[i].ToString(), null);//o对象,reader[i]对象的新值,索引器空的 } return o; } |
|