示例#1
0
        /// <summary>
        /// Tries to convert the input object to the output type using TypeConverters. If the destination type is a superclass of the input type,
        /// if will use <see cref="Convert.ChangeType(object,System.Type)"/>.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="destinationType">Type of the destination.</param>
        /// <returns></returns>
        public static AttemptTuple <object> TryConvertTo(this object input, Type destinationType)
        {
            if (input == null)
            {
                return(AttemptTuple <object> .False);
            }

            if (destinationType == typeof(object))
            {
                return(new AttemptTuple <object>(true, input));
            }

            if (input.GetType() == destinationType)
            {
                return(new AttemptTuple <object>(true, input));
            }

            if (!destinationType.IsGenericType || destinationType.GetGenericTypeDefinition() != typeof(Nullable <>))
            {
                if (TypeFinder.IsTypeAssignableFrom(destinationType, input.GetType()) &&
                    TypeFinder.IsTypeAssignableFrom <IConvertible>(input))
                {
                    var casted = Convert.ChangeType(input, destinationType);
                    return(new AttemptTuple <object>(true, casted));
                }
            }

            var inputConverter = TypeDescriptor.GetConverter(input);

            if (inputConverter != null)
            {
                if (inputConverter.CanConvertTo(destinationType))
                {
                    return(new AttemptTuple <object>(true, inputConverter.ConvertTo(input, destinationType)));
                }
            }

            if (destinationType == typeof(bool))
            {
                var boolConverter = new CustomBooleanTypeConverter();
                if (boolConverter.CanConvertFrom(input.GetType()))
                {
                    return(new AttemptTuple <object>(true, boolConverter.ConvertFrom(input)));
                }
            }

            var outputConverter = TypeDescriptor.GetConverter(destinationType);

            if (outputConverter != null)
            {
                if (outputConverter.CanConvertFrom(input.GetType()))
                {
                    return(new AttemptTuple <object>(true, outputConverter.ConvertFrom(input)));
                }
            }

            try
            {
                if (TypeFinder.IsTypeAssignableFrom <IConvertible>(input))
                {
                    var casted = Convert.ChangeType(input, destinationType);
                    return(new AttemptTuple <object>(true, casted));
                }
            }
            catch (Exception)
            {
                /* Swallow */
            }

            return(AttemptTuple <object> .False);
        }
示例#2
0
 /// <summary>
 /// Determines whether the specified actual type is type.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="actualType">The actual type.</param>
 /// <returns>
 ///   <c>true</c> if the specified actual type is type; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsType <T>(this Type actualType)
 {
     return(TypeFinder.IsTypeAssignableFrom <T>(actualType));
 }
示例#3
0
 private static bool IsNotCloneable(MemberInfo info)
 {
     return(TypeFinder.IsTypeAssignableFrom <Delegate>(info.DeclaringType));
 }