Exemplo n.º 1
0
        /// <summary>
        /// 将数据行转换为指定类型的对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="row">数据行</param>
        /// <param name="excludeProperties">需要排除的属性名称列表</param>
        /// <returns></returns>
        internal static T ToObject <T>(DataRow row, params string[] excludeProperties)
        {
            if (row == null)
            {
                return(default(T));
            }

            //获取限定类型的类类型
            Type targetType = typeof(T);
            //通过反射中的激活器根据指定类型的无参构造函数创建对象实例
            T resultObject = Activator.CreateInstance <T>();
            //根据当前数据行获取拥有其架构的数据表对象
            DataTable table = row.Table;

            //指示属性为公开的、含有Set访问的、含有实例的、忽略大小写的
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase;

            foreach (DataColumn column in table.Columns)
            {
                if (excludeProperties.Contains <string>(column.ColumnName))
                {
                    continue;
                }
                //根据列名称获取目标类型中的属性对象
                PropertyInfo targetProperty = targetType.GetProperty(column.ColumnName, bindingFlags);
                //如果未找到对象则抛出异常
                if (targetProperty == null)
                {
                    throw new PropertyNotFoundException(targetType.FullName, column.ColumnName);
                }

                //获取用户自定义特性
                Attribute attribute = targetProperty.GetCustomAttribute(typeof(NonReflectionAttribute));
                if (attribute != null)
                {
                    continue;
                }

                //获取指定属性的类类型
                Type propertyType = targetProperty.PropertyType;

                object parameters  = null;
                bool   convertFlag = true;

                try
                {
                    parameters = DataTypeHelper.ChangeType(propertyType, row[column]);
                }
                catch { convertFlag = false; }

                //如果数据类型转换成功,则将单元格中的值保存到指定属性中
                if (convertFlag)
                {
                    targetType.InvokeMember(column.ColumnName, bindingFlags, null, resultObject, new object[] { parameters });
                }
            }

            return(resultObject);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取指定类型配置信息,并转换为指定类型
        /// </summary>
        /// <typeparam name="T">需要转换的类型</typeparam>
        /// <param name="key">配置键</param>
        /// <returns></returns>
        public static T Get <T>(string key)
        {
            string setting = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(setting))
            {
                return(default(T));
            }
            return((T)DataTypeHelper.ChangeType(typeof(T), setting));
        }
Exemplo n.º 3
0
        public T Get <T>(string key)
        {
            if (typeof(T) == typeof(string))
            {
                HttpCookie cookie = Context.Request.Cookies[key];
                if (cookie != null)
                {
                    return((T)DataTypeHelper.ChangeType(typeof(T), cookie.Value));
                }
            }

            return(default(T));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 获取单个结果
        /// </summary>
        /// <typeparam name="T">结果类型</typeparam>
        /// <param name="helper">数据库操作辅助对象</param>
        /// <param name="tableName">表名</param>
        /// <param name="columnName">列名称</param>
        /// <param name="param">数据库操作参数</param>
        /// <returns></returns>
        public static T GetSingleResult <T>(this SqlHelper helper, string tableName, string columnName, params SqlParameter[] param)
        {
            string commandText = string.Format("SELECT TOP 1 {0} FROM {1}(NOLOCK)", columnName, tableName);

            if (param != null)
            {
                commandText += " WHERE 1=1 ";
                foreach (var item in param)
                {
                    string key   = item.ParameterName.StartsWith("@") ? item.ParameterName.Substring(1) : item.ParameterName;
                    string value = item.ParameterName.StartsWith("@") ? item.ParameterName : "@" + item.ParameterName;
                    commandText += string.Format("AND {0}={1}", key, value);
                }
            }

            object tmpObjResult = helper.ExecuteScalar(CommandType.Text, commandText, param);

            return((T)DataTypeHelper.ChangeType(typeof(T), tmpObjResult));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 获取单个结果
        /// </summary>
        /// <typeparam name="T">结果类型</typeparam>
        /// <param name="helper">数据库操作辅助对象</param>
        /// <param name="commandType">数据库操作类型</param>
        /// <param name="commandText">数据库操作语句</param>
        /// <param name="param">数据库操作参数</param>
        /// <returns></returns>
        public static T GetSingleResult <T>(this SqlHelper helper, CommandType commandType, string commandText, params SqlParameter[] param)
        {
            object tmpObjResult = helper.ExecuteScalar(commandType, commandText, param);

            return((T)DataTypeHelper.ChangeType(typeof(T), tmpObjResult));
        }