Esempio n. 1
0
        /// <summary>
        ///     Composes another <see cref="ValueConverter" /> instance with this one such that
        ///     the result of the first conversion is used as the input to the second conversion.
        /// </summary>
        /// <param name="secondConverter"> The second converter. </param>
        /// <returns> The composed converter. </returns>
        public virtual ValueConverter ComposeWith(
            [CanBeNull] ValueConverter secondConverter)
        {
            if (secondConverter == null)
            {
                return(this);
            }

            if (StoreType.UnwrapNullableType() != secondConverter.ModelType.UnwrapNullableType())
            {
                throw new ArgumentException(
                          CoreStrings.ConvertersCannotBeComposed(
                              ModelType.ShortDisplayName(),
                              StoreType.ShortDisplayName(),
                              secondConverter.ModelType.ShortDisplayName(),
                              secondConverter.StoreType.ShortDisplayName()));
            }

            var firstConverter
                = StoreType.IsNullableType() &&
                  !secondConverter.ModelType.IsNullableType()
                    ? ComposeWith(
                      (ValueConverter)Activator.CreateInstance(
                          typeof(CastingConverter <,>).MakeGenericType(
                              StoreType,
                              secondConverter.ModelType),
                          MappingHints))
                    : this;

            return((ValueConverter)Activator.CreateInstance(
                       typeof(CompositeValueConverter <, ,>).MakeGenericType(
                           firstConverter.ModelType,
                           firstConverter.StoreType,
                           secondConverter.StoreType),
                       firstConverter,
                       secondConverter,
                       firstConverter.MappingHints.With(secondConverter.MappingHints)));
        }