protected virtual void SetProperty(T obj, MarkupProperty markupProperty) { var type = obj.GetType(); var property = type.GetProperty(markupProperty.Name); if (property == null) { throw new InvalidOperationException($"{type.Name} does not have a property {markupProperty.Name}."); } SetPropertyValue(obj, property, markupProperty); }
protected override void SetProperty(T obj, MarkupProperty markupProperty) { var property = SolidProperty.GetProperty(obj.GetType(), markupProperty.Name); if (property == null) { base.SetProperty(obj, markupProperty); return; } if (markupProperty.Type == MarkupPropertyType.Binding) { SolidProperty.Bind(property, obj, markupProperty.Value); return; } if (property.Metadata.IsExported == false) { throw new NotSupportedException("Setting a non-exported property is not supported."); } property.SetValue(obj, ConvertPropertyType(property.PropertyType, markupProperty)); }
protected object ConvertPropertyType(Type targetType, MarkupProperty source) { if (converters.ContainsKey(targetType)) { var converter = converters[targetType]; return(converter.ConvertFromInvariantString(source.Value)); } else if (targetType.IsEnum) { if (source.Type != MarkupPropertyType.Enumeration) { throw new InvalidOperationException($"Expected enumeration value for {source.Name}."); } return(Enum.Parse(targetType, source.Value)); } else if (targetType == typeof(string)) { if (source.Type != MarkupPropertyType.String) { throw new InvalidOperationException($"Expected string value for {source.Name}."); } return(source.Value); } else if (targetType.IsPrimitive) { if (source.Type != MarkupPropertyType.Number) { throw new InvalidOperationException($"Expected number value for {source.Name}."); } return(Convert.ChangeType(source.Value, targetType, CultureInfo.InvariantCulture)); } else { throw new NotSupportedException($"{targetType.Name} is not a supported type."); } }
private void SetPropertyValue(object obj, PropertyInfo target, MarkupProperty source) { target.SetValue(obj, ConvertPropertyType(target.PropertyType, source)); }