欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

将DataRow转成指定类型的类,并返回这个类的对象(带值)

程序员文章站 2022-05-22 14:19:32
 ///          ///&nbs...
 /// <summary>
        /// 将datarow转换成指定类型
        /// </summary>
        /// <param name="pdatarow"></param>
        /// <param name="ptype">实体类</param>
        /// <returns></returns>
        private object converttoentity(datarow pdatarow, type ptype)
        {
            object entity = null;
            object provalue = null;
            propertyinfo propertyinfo = null;
            try
            {
                if (pdatarow != null)
                {
                    //动态创建类的实例
                    entity = activator.createinstance(ptype);
                    foreach (datacolumn dc in pdatarow.table.columns)
                    {
                        //忽略绑定时的大小写
                        propertyinfo = ptype.getproperty(dc.columnname, bindingflags.public | bindingflags.instance | bindingflags.ignorecase);
                        provalue = pdatarow[dc];
                        //当值不为空时