Example #1
0
        private void SetPropertyValue(string prop, string strValue, object value, IServiceProvider serviceProvider)
        {
            MethodInfo setter;

            if (prop == null)
            {
                //implicit property
                var t = markupExtension.GetType();
                prop = ApplyPropertiesVisitor.GetContentPropertyName(t.GetTypeInfo());
                if (prop == null)
                {
                    return;
                }
                try {
                    setter = t.GetRuntimeProperty(prop).SetMethod;
                }
                catch (AmbiguousMatchException e) {
                    throw new XamlParseException($"Multiple properties with name  '{t}.{prop}' found.", serviceProvider, innerException: e);
                }
            }
            else
            {
                try {
                    setter = markupExtension.GetType().GetRuntimeProperty(prop).SetMethod;
                }
                catch (AmbiguousMatchException e) {
                    throw new XamlParseException($"Multiple properties with name  '{markupExtension.GetType()}.{prop}' found.", serviceProvider, innerException: e);
                }
            }
            if (value == null && strValue != null)
            {
                try {
                    value = strValue.ConvertTo(markupExtension.GetType().GetRuntimeProperty(prop).PropertyType,
                                               (Func <TypeConverter>)null, serviceProvider, out Exception converterException);
                    if (converterException != null)
                    {
                        throw converterException;
                    }
                }
                catch (AmbiguousMatchException e) {
                    throw new XamlParseException($"Multiple properties with name  '{markupExtension.GetType()}.{prop}' found.", serviceProvider, innerException: e);
                }
            }

            setter.Invoke(markupExtension, new[] { value });
        }