in src/Microsoft.Xaml.Behaviors/Core/ChangePropertyAction.cs [411:470]
private static object TryAddition(object currentValue, object value)
{
object returnValue = null;
Type valueType = value.GetType();
Type additiveType = currentValue.GetType();
MethodInfo uniqueAdditionOperation = null;
object convertedValue = value;
foreach (MethodInfo additionOperation in additiveType.GetMethods())
{
if (string.Compare(additionOperation.Name, "op_Addition", StringComparison.Ordinal) != 0)
{
continue;
}
ParameterInfo[] parameters = additionOperation.GetParameters();
Debug.Assert(parameters.Length == 2, "op_Addition is expected to have 2 parameters");
Type secondParameterType = parameters[1].ParameterType;
if (!parameters[0].ParameterType.IsAssignableFrom(additiveType))
{
continue;
}
else if (!secondParameterType.IsAssignableFrom(valueType))
{
TypeConverter additionConverter = TypeConverterHelper.GetTypeConverter(secondParameterType);
if (additionConverter.CanConvertFrom(valueType))
{
convertedValue = TypeConverterHelper.DoConversionFrom(additionConverter, value);
}
else
{
continue;
}
}
if (uniqueAdditionOperation != null)
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
ExceptionStringTable.ChangePropertyActionAmbiguousAdditionOperationExceptionMessage,
additiveType.Name));
}
uniqueAdditionOperation = additionOperation;
}
if (uniqueAdditionOperation != null)
{
returnValue = uniqueAdditionOperation.Invoke(null, new object[] { currentValue, convertedValue });
}
else
{
// we couldn't figure out how to add, so pack it up and just set value
returnValue = value;
}
return returnValue;
}