Beispiel #1
0
        /// <summary>
        /// 填充一个实体类的属性
        /// </summary>
        /// <param name="entity">实体类的实例</param>
        /// <param name="propertyInfo">要填充的属性的信息</param>
        /// <param name="sourceValue">从数据源获取到的属性的只</param>
        private static void FillProperty(object entity, PropertyInfo propertyInfo, object sourceValue)
        {
            var result = EntityParser.GetTypeConverter(propertyInfo);

            if (result == null)
            {
                sourceValue = sourceValue.To(propertyInfo.PropertyType);
                propertyInfo.SetValue(entity, sourceValue, null);
            }
            else
            {
                var propertyValue = result.ConvertFrom(sourceValue);
                propertyInfo.SetValue(entity, propertyValue, null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 转换属性的值
        /// </summary>
        /// <param name="propertyInfo">属性信息</param>
        /// <param name="source">从数据源获取到的值</param>
        /// <param name="destination">目标类型</param>
        /// <returns></returns>
        public static object ConvertPropertyValue(PropertyInfo propertyInfo, object source, DBType destination)
        {
            Type destinationType = null;

            switch (destination)
            {
            case DBType.String:
                destinationType = typeof(string);
                break;

            case DBType.Int:
                destinationType = typeof(int);
                break;

            case DBType.Decimal:
                destinationType = typeof(decimal);
                break;

            case DBType.DateTime:
                destinationType = typeof(DateTime);
                break;

            case DBType.BigInt:
                destinationType = typeof(long);
                break;
            }

            if (destinationType == null)
            {
                return(null);
            }

            var converter = EntityParser.GetTypeConverter(propertyInfo);              //获取类型转换器

            if (converter != null)
            {
                return(converter.ConvertTo(source, destinationType));                 //使用自定义类型转换器将值转换为另一种类型
            }

            return(source.To(destinationType));
        }