将DataRow转成指定类型的类,并返回这个类的对象(带值)
程序员文章站
2022-12-02 11:59:20
/// ///&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];
//当值不为空时
/// 将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];
//当值不为空时
上一篇: 利用C#远程存取Access数据库