分享

DataReader转 object对象

 昵称5414326 2011-04-22
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;
            }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多