using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
namespace ObjectMappingControls
{
public class CommonMapping
{
private static void ReaderToModel(IDataReader reader, object Model)
{
for (int i = 0; i < reader.FieldCount; i++)
{
System.Reflection.PropertyInfo propertyInfo = Model.GetType().GetProperty(reader.GetName(i));
if (propertyInfo != null)
{
if (reader.GetValue(i) != DBNull.Value)
{
if (propertyInfo.PropertyType.IsEnum)
{
propertyInfo.SetValue(Model, Enum.ToObject(propertyInfo.PropertyType, reader.GetValue(i)), null);
}
else
{
propertyInfo.SetValue(Model, reader.GetValue(i), null);
}
}
}
}
}
}
}
reader反射到Model
最新推荐文章于 2025-04-09 15:25:44 发布
本文介绍了一种将IDataReader中的数据映射到.NET对象的方法。通过反射获取对象属性,并将其与DataReader中的字段进行匹配,实现了从数据库读取的数据自动填充到对象属性中。特别注意到了枚举类型的转换处理。

1142

被折叠的 条评论
为什么被折叠?



