Esempio n. 1
0
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Converts the specified value.
        /// </summary>
        /// <param name="request">A description of the conversion request.</param>
        /// <param name="result">The converted value.</param>
        /// <returns><see langword="True"/> if the conversion succeeded or was unnecessary, otherwise <see langword="false"/>.</returns>
        public bool Convert(ConversionRequest request, out object result)
        {
            result = request.Value;

            if (request.Value == null)
            {
                return(true);
            }

            if (request.TargetType.IsInstanceOfType(request.Value) || request.TargetType.IsAssignableFrom(request.SourceType))
            {
                return(true);
            }

#if NO_TYPE_CONVERTERS
            try
            {
                result = System.Convert.ChangeType(request.Value, request.TargetType, CultureInfo.CurrentCulture);
                return(true);
            }
            catch (InvalidCastException)
            {
                return(false);
            }
#else
            TypeConverter converter = TypeDescriptor.GetConverter(request.TargetType);

            if (converter.CanConvertFrom(request.SourceType))
            {
                result = converter.ConvertFrom(request.Value);
                return(true);
            }

            converter = TypeDescriptor.GetConverter(request.SourceType);

            if (converter.CanConvertTo(request.TargetType))
            {
                result = converter.ConvertTo(request.Value, request.TargetType);
                return(true);
            }

            return(false);
#endif
        }
		/*----------------------------------------------------------------------------------------*/
		/// <summary>
		/// Converts the specified value.
		/// </summary>
		/// <param name="request">A description of the conversion request.</param>
		/// <param name="result">The converted value.</param>
		/// <returns><see langword="True"/> if the conversion succeeded or was unnecessary, otherwise <see langword="false"/>.</returns>
		public bool Convert(ConversionRequest request, out object result)
		{
			result = request.Value;

			if (request.Value == null)
				return true;

			if (request.TargetType.IsInstanceOfType(request.Value) || request.TargetType.IsAssignableFrom(request.SourceType))
				return true;

#if NO_TYPE_CONVERTERS
			try
			{
				result = System.Convert.ChangeType(request.Value, request.TargetType, CultureInfo.CurrentCulture);
				return true;
			}
			catch (InvalidCastException)
			{
				return false;
			}
#else
			TypeConverter converter = TypeDescriptor.GetConverter(request.TargetType);

			if (converter.CanConvertFrom(request.SourceType))
			{
				result = converter.ConvertFrom(request.Value);
				return true;
			}

			converter = TypeDescriptor.GetConverter(request.SourceType);

			if (converter.CanConvertTo(request.TargetType))
			{
				result = converter.ConvertTo(request.Value, request.TargetType);
				return true;
			}

			return false;
#endif
		}
Esempio n. 3
0
 /*----------------------------------------------------------------------------------------*/
 /// <summary>
 /// Determines whether the specified object matches the condition.
 /// </summary>
 /// <param name="value">The object to test.</param>
 /// <returns><see langword="True"/> if the object matches, otherwise <see langword="false"/>.</returns>
 public bool Matches(ConversionRequest value)
 {
     return(true);
 }
		/*----------------------------------------------------------------------------------------*/
		/// <summary>
		/// Determines whether the specified object matches the condition.
		/// </summary>
		/// <param name="value">The object to test.</param>
		/// <returns><see langword="True"/> if the object matches, otherwise <see langword="false"/>.</returns>
		public bool Matches(ConversionRequest value)
		{
			return true;
		}