コード例 #1
0
        internal static void SetValue(object target, PropertyInfo targetProperty, object value)
        {
            if (value == null)
            {
                if (TypeExpress.AllowNullValue(targetProperty.PropertyType))
                {
                    targetProperty.SetValue(target, null);
                }
                return;
            }
            Type sourceType = value.GetType();
            Type targetType = targetProperty.PropertyType;

            if (sourceType == targetType)
            {
                targetProperty.SetValue(target, value);
                return;
            }
            if (ConvertExpress.CanConvert(sourceType, targetType))
            {
                object convertedValue = ConvertExpress.Convert(value, targetType);
                if (convertedValue == null)
                {
                    if (TypeExpress.AllowNullValue(targetProperty.PropertyType))
                    {
                        targetProperty.SetValue(target, null);
                    }
                }
                else
                {
                    targetProperty.SetValue(target, convertedValue);
                }
            }
        }
コード例 #2
0
        public static TResult Convert <TResult>(object value)
        {
            Type targetType = typeof(TResult);

            if (value == null)
            {
                if (TypeExpress.AllowNullValue(targetType))
                {
                    return(default(TResult));
                }
                throw new InvalidCastException();
            }
            Type sourceType = value.GetType();

            if (sourceType == targetType)
            {
                return((TResult)value);
            }
            Convert <object, object> func = s_mappingDictionary.Value.GetMapping(sourceType, targetType);

            if (func != null)
            {
                object convertedValue = func(value);
                if (convertedValue == null)
                {
                    if (TypeExpress.AllowNullValue(targetType))
                    {
                        return(default(TResult));
                    }
                }
                else
                {
                    return((TResult)convertedValue);
                }
            }
            throw new InvalidCastException();
            ;
        }