sqlhelper.putobjectproperties.cs
来自「该项目中对 SQLHelper 类进行了简单封装」· CS 代码 · 共 171 行
CS
171 行
/*
* SQLHelper.PutObjectProperties.cs @Microsoft Visual Studio 2005 <.NET Framework 2.0>
* AfritXia
* 2007-05-03
*
* Copyright(c) http://www.AfritXia.NET/
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Reflection;
namespace NET.AfritXia.DBUtility
{
partial class SQLHelper
{
// 字典管理器
private DictionaryManager m_dictionaryManager = new DictionaryManager();
/// <summary>
/// 设置或获取字典供应者
/// </summary>
public IOrmDictionaryProvider DictionaryProvider
{
set
{
this.m_dictionaryManager.DictionaryProvider = value;
}
get
{
return this.m_dictionaryManager.DictionaryProvider;
}
}
/// <summary>
/// 从数据源中读取数据,并建立一个新的对象
/// </summary>
/// <typeparam name="T">对象类型模板</typeparam>
/// <param name="dr">数据源</param>
/// <returns>新对象实例</returns>
public T CreateObject<T>(DbDataReader dr)
{
// 建立一个新的对象
T obj = Activator.CreateInstance<T>();
// 设置对象实例
this.PutObjectProperties(obj, dr);
return obj;
}
/// <summary>
/// 从数据源中读取数据,并设置到对象属性
/// </summary>
/// <param name="obj">所要设置的对象</param>
/// <param name="dr">数据源</param>
public void PutObjectProperties(object obj, DbDataReader dr)
{
// 获取对象模型字典
Dictionary<string, string> dictionary = this.m_dictionaryManager.GetOrmDictionary(obj.GetType());
// 将数据库字段值添加到对象属性
for (int i = 0; i < dr.FieldCount; i++)
{
// 获取数据库字段值
object dbColumnVal = dr.GetValue(i);
// 当前列的值为空值,则直接进入下次循环
if (dbColumnVal == null || (dbColumnVal is DBNull))
continue;
// 获取列名称
string dbColumnName = dr.GetName(i);
// 对象属性名称
string objectProperty = dbColumnName;
if (dictionary != null)
{
if (!dictionary.ContainsKey(dbColumnName))
continue;
// 获取对象属性名称
objectProperty = dictionary[dbColumnName] as string;
}
if (String.IsNullOrEmpty(objectProperty))
continue;
// 将对象属性以 . 分割,以便获取内置对象
// 例如:"Topic.Content.ContentString",被分割的结果为:
// "Topic", "Content", "ContentString"
string[] propertyNames = objectProperty.Split('.');
// 当前访问的属性名称
string currPropName = null;
// 对象中的内置对象
object innerObj = obj;
// 内置对象的类型
Type innerObjType = innerObj.GetType();
// 遍历内置对象,但最深的内置对象除外
// 例如:Topic.Content.ContentString,只遍历:
// "Topic", "Content"
// "ContentString" 不遍历
for (int n = 0; n < propertyNames.Length - 1; n++)
{
currPropName = propertyNames[n];
// 获取内置对象的属性信息
PropertyInfo propertyInfo = innerObjType.GetProperty(currPropName);
// 如果属性信息不存在,则抛出 UndefPropertyException 异常
if (propertyInfo == null)
throw new UndefPropertyException(currPropName, innerObjType);
// 获取属性信息的值
object innerObjVal = propertyInfo.GetValue(innerObj, null);
// 如果属性信息的值为空,则新建
if (innerObjVal == null)
{
// 获取默认构造器信息
ConstructorInfo constructorInfo = propertyInfo.PropertyType.GetConstructor(Type.EmptyTypes);
// 如果未定义默认构造器或默认构造器不为公开,则抛出 UndefConstructorException
if (constructorInfo == null || !constructorInfo.IsPublic)
throw new UndefConstructorException(propertyInfo.PropertyType);
// 根据属性信息类型,新建一个实例
innerObjVal = Activator.CreateInstance(propertyInfo.PropertyType);
// 将新值设置到内置对象
propertyInfo.SetValue(innerObj, innerObjVal, null);
}
// 递进到下一个内置对象
innerObj = innerObjVal;
// 获取内置对象的类型
innerObjType = innerObj.GetType();
}
currPropName = propertyNames[propertyNames.Length - 1];
// 获取最深的内置对象,该内置对象应该是一个值类型的对象
PropertyInfo valuePropertyInfo = innerObjType.GetProperty(currPropName);
// 如果内置对象属性为空,则抛出 UndefPropertyException 异常
if (valuePropertyInfo == null)
throw new UndefPropertyException(currPropName, innerObjType);
if (valuePropertyInfo.PropertyType.IsEnum)
{
// 获取枚举值
object enumVal = Enum.Parse(valuePropertyInfo.PropertyType,
dbColumnVal.ToString(), false);
// 设置属性值,如果该属性是枚举值
valuePropertyInfo.SetValue(innerObj, enumVal, null);
}
else
{
// 设置属性值
valuePropertyInfo.SetValue(innerObj, dbColumnVal, null);
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?