protected override void Invoke()

in src/Microsoft.Xaml.Behaviors/Core/ChangePropertyAction.cs [108:192]


        protected override void Invoke(object parameter)
        {
            if (this.AssociatedObject == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(this.PropertyName))
            {
                return;
            }
            if (this.Target == null)
            {
                return;
            }

            Type targetType = this.Target.GetType();
            PropertyInfo propertyInfo = targetType.GetProperty(this.PropertyName);
            this.ValidateProperty(propertyInfo);

            object newValue = this.Value;
            TypeConverter converter = TypeConverterHelper.GetTypeConverter(propertyInfo.PropertyType);

            Exception innerException = null;
            try
            {
                if (this.Value != null)
                {
                    if (converter != null && converter.CanConvertFrom(this.Value.GetType()))
                    {
                        newValue = converter.ConvertFrom(context: null, culture: CultureInfo.InvariantCulture, value: this.Value);
                    }
                    else
                    {
                        // Try asking the value if it can convert itself to the target property
                        converter = TypeConverterHelper.GetTypeConverter(this.Value.GetType());
                        if (converter != null && converter.CanConvertTo(propertyInfo.PropertyType))
                        {
                            newValue = converter.ConvertTo(
                                context: null,
                                culture: CultureInfo.InvariantCulture,
                                value: this.Value,
                                destinationType: propertyInfo.PropertyType);
                        }
                    }
                }

                // If a duration is set, we should animate this value.
                if (this.Duration.HasTimeSpan)
                {
                    this.ValidateAnimationPossible(targetType);
                    object fromValue = ChangePropertyAction.GetCurrentPropertyValue(this.Target, propertyInfo);
                    this.AnimatePropertyChange(propertyInfo, fromValue, newValue);
                }
                else
                {
                    if (this.Increment)
                    {
                        newValue = this.IncrementCurrentValue(propertyInfo);
                    }
                    propertyInfo.SetValue(this.Target, newValue, new object[0]);
                }
            }
            catch (FormatException e)
            {
                innerException = e;
            }
            catch (ArgumentException e)
            {
                innerException = e;
            }
            catch (MethodAccessException e)
            {
                innerException = e;
            }
            if (innerException != null)
            {
                throw new ArgumentException(string.Format(
                    CultureInfo.CurrentCulture,
                    ExceptionStringTable.ChangePropertyActionCannotSetValueExceptionMessage,
                    this.Value != null ? this.Value.GetType().Name : "null",
                    this.PropertyName,
                    propertyInfo.PropertyType.Name),
                    innerException);
            }
        }