2 using System.Collections;
3 using System.Collections.Generic;
5 using System.Reflection;
7 using Tizen.NUI.Binding.Internals;
8 using Tizen.NUI.Binding;
9 using Tizen.NUI.StyleSheets;
11 using static System.String;
13 namespace Tizen.NUI.Xaml
15 internal class ApplyPropertiesVisitor : IXamlNodeVisitor
17 public static readonly IList<XmlName> Skips = new List<XmlName> {
19 XmlName.xTypeArguments,
21 XmlName.xFactoryMethod,
26 public ApplyPropertiesVisitor(HydrationContext context, bool stopOnResourceDictionary = false)
29 StopOnResourceDictionary = stopOnResourceDictionary;
32 Dictionary<INode, object> Values => Context.Values;
33 HydrationContext Context { get; }
35 public TreeVisitingMode VisitingMode => TreeVisitingMode.BottomUp;
36 public bool StopOnDataTemplate => true;
37 public bool StopOnResourceDictionary { get; }
38 public bool VisitNodeOnDataTemplate => true;
39 public bool SkipChildren(INode node, INode parentNode) => false;
40 public bool IsResourceDictionary(ElementNode node) => typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]);
42 public void Visit(ValueNode node, INode parentNode)
44 var parentElement = parentNode as IElementNode;
45 var value = Values[node];
46 var source = Values[parentNode];
49 if (TryGetPropertyName(node, parentNode, out propertyName))
51 if (TrySetRuntimeName(propertyName, source, value, node))
53 if (Skips.Contains(propertyName))
55 if (parentElement.SkipProperties.Contains(propertyName))
57 if (propertyName.Equals(XamlParser.McUri, "Ignorable"))
59 SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
61 else if (IsCollectionItem(node, parentNode) && parentNode is IElementNode)
63 // Collection element, implicit content, or implicit collection element.
64 var contentProperty = GetContentPropertyName(Context.Types[parentElement].GetTypeInfo());
65 if (contentProperty != null)
67 var name = new XmlName(((ElementNode)parentNode).NamespaceURI, contentProperty);
68 if (Skips.Contains(name))
70 if (parentElement.SkipProperties.Contains(propertyName))
72 SetPropertyValue(source, name, value, Context.RootElement, node, Context, node);
77 public void Visit(MarkupNode node, INode parentNode)
81 public void Visit(ElementNode node, INode parentNode)
84 if (TryGetPropertyName(node, parentNode, out propertyName) && propertyName == XmlName._CreateContent)
86 var s0 = Values[parentNode];
87 if (s0 is ElementTemplate)
89 SetTemplate(s0 as ElementTemplate, node);
94 var parentElement = parentNode as IElementNode;
95 propertyName = XmlName.Empty;
97 //Simplify ListNodes with single elements
98 var pList = parentNode as ListNode;
99 if (pList != null && pList.CollectionItems.Count == 1)
101 propertyName = pList.XmlName;
102 parentNode = parentNode.Parent;
103 parentElement = parentNode as IElementNode;
106 var value = Values[node];
108 if (propertyName != XmlName.Empty || TryGetPropertyName(node, parentNode, out propertyName))
110 if (Skips.Contains(propertyName))
112 if (parentElement == null)
114 if (parentElement.SkipProperties.Contains(propertyName))
117 var source = Values[parentNode];
118 ProvideValue(ref value, node, source, propertyName);
119 SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
121 else if (IsCollectionItem(node, parentNode) && parentNode is IElementNode)
123 var source = Values[parentNode];
124 ProvideValue(ref value, node, source, XmlName.Empty);
125 string contentProperty;
126 Exception xpe = null;
127 var xKey = node.Properties.ContainsKey(XmlName.xKey) ? ((ValueNode)node.Properties[XmlName.xKey]).Value as string : null;
130 if (xpe == null && TryAddToResourceDictionary(source as ResourceDictionary, value, xKey, node, out xpe))
133 // Dictionary with string key
134 if (xpe == null && xKey != null)
136 var indexer = GetIndexer(source, typeof(string), value.GetType());
139 indexer.SetValue(source, value, new[] { xKey });
144 // Collection element, implicit content, or implicit collection element.
145 if (xpe == null && typeof(IEnumerable).IsAssignableFrom(Context.Types[parentElement]) && Context.Types[parentElement].GetRuntimeMethods().Any(mi => mi.Name == "Add" && mi.GetParameters().Length == 1))
148 Context.Types[parentElement].GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1);
150 addMethod?.Invoke(source, new[] { value });
153 if (xpe == null && Context.Types[parentElement].GetRuntimeMethods().Any(mi => mi.Name == "Add" && mi.GetParameters().Length == 1))
155 //if there are similar parameters in the function, this will exist issue.
156 var addMethod = Context.Types[parentElement].GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1);
157 if (addMethod != null) addMethod.Invoke(source, new[] { value });
160 if (xpe == null && (contentProperty = GetContentPropertyName(Context.Types[parentElement].GetTypeInfo())) != null)
162 var name = new XmlName(node.NamespaceURI, contentProperty);
163 if (Skips.Contains(name))
165 if (parentElement.SkipProperties.Contains(propertyName))
168 SetPropertyValue(source, name, value, Context.RootElement, node, Context, node);
172 xpe = xpe ?? new XamlParseException($"Can not set the content of {((IElementNode)parentNode).XmlType.Name} as it doesn't have a ContentPropertyAttribute", node);
173 if (Context.ExceptionHandler != null)
174 Context.ExceptionHandler(xpe);
177 else if (IsCollectionItem(node, parentNode) && parentNode is ListNode)
179 var source = Values[parentNode.Parent];
180 ProvideValue(ref value, node, source, XmlName.Empty);
181 var parentList = (ListNode)parentNode;
182 if (Skips.Contains(parentList.XmlName))
184 Exception xpe = null;
185 var xKey = node.Properties.ContainsKey(XmlName.xKey) ? ((ValueNode)node.Properties[XmlName.xKey]).Value as string : null;
188 var collection = GetPropertyValue(source, parentList.XmlName, Context, parentList, out _) as IEnumerable;
189 if (collection == null)
190 xpe = new XamlParseException($"Property {parentList.XmlName.LocalName} is null or is not IEnumerable", node);
192 if (xpe == null && TryAddToResourceDictionary(collection as ResourceDictionary, value, xKey, node, out xpe))
195 MethodInfo addMethod;
196 if (xpe == null && (addMethod = collection.GetType().GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1)) != null)
198 addMethod.Invoke(collection, new[] { Values[node] });
201 xpe = xpe ?? new XamlParseException($"Value of {parentList.XmlName.LocalName} does not have a Add() method", node);
202 if (Context.ExceptionHandler != null)
203 Context.ExceptionHandler(xpe);
211 public void Visit(RootNode node, INode parentNode)
215 public void Visit(ListNode node, INode parentNode)
219 public static bool TryGetPropertyName(INode node, INode parentNode, out XmlName name)
221 name = default(XmlName);
222 var parentElement = parentNode as IElementNode;
223 if (parentElement == null)
225 foreach (var kvp in parentElement.Properties)
227 if (kvp.Value != node)
235 internal static bool IsCollectionItem(INode node, INode parentNode)
237 var parentList = parentNode as IListNode;
238 if (parentList == null)
240 return parentList.CollectionItems.Contains(node);
243 internal static string GetContentPropertyName(System.Reflection.TypeInfo typeInfo)
245 while (typeInfo != null)
247 var propName = GetContentPropertyName(typeInfo.CustomAttributes);
248 if (propName != null)
250 typeInfo = typeInfo?.BaseType?.GetTypeInfo();
255 void ProvideValue(ref object value, ElementNode node, object source, XmlName propertyName)
257 var markupExtension = value as IMarkupExtension;
258 var valueProvider = value as IValueProvider;
260 if (markupExtension == null && valueProvider == null)
263 XamlServiceProvider serviceProvider = null;
264 if (value.GetType().GetTypeInfo().GetCustomAttribute<AcceptEmptyServiceProviderAttribute>() == null)
265 serviceProvider = new XamlServiceProvider(node, Context);
267 if (serviceProvider != null && serviceProvider.IProvideValueTarget is XamlValueTargetProvider && propertyName != XmlName.Empty)
269 (serviceProvider.IProvideValueTarget as XamlValueTargetProvider).TargetProperty = GetTargetProperty(source, propertyName, Context, node);
272 if (markupExtension != null)
273 value = markupExtension.ProvideValue(serviceProvider);
274 else if (valueProvider != null)
275 value = valueProvider.ProvideValue(serviceProvider);
278 static string GetContentPropertyName(IEnumerable<CustomAttributeData> attributes)
280 var contentAttribute =
281 attributes.FirstOrDefault(cad => ContentPropertyAttribute.ContentPropertyTypes.Contains(cad.AttributeType.FullName));
282 if (contentAttribute == null || contentAttribute.ConstructorArguments.Count != 1)
284 if (contentAttribute.ConstructorArguments[0].ArgumentType == typeof(string))
285 return (string)contentAttribute.ConstructorArguments[0].Value;
289 static bool GetRealNameAndType(ref Type elementType, string namespaceURI, ref string localname,
290 HydrationContext context, IXmlLineInfo lineInfo)
292 var dotIdx = localname.IndexOf('.');
295 var typename = localname.Substring(0, dotIdx);
296 localname = localname.Substring(dotIdx + 1);
297 XamlParseException xpe;
298 elementType = XamlParser.GetElementType(new XmlType(namespaceURI, typename, null), lineInfo,
299 context.RootElement.GetType().GetTypeInfo().Assembly, out xpe);
308 static BindableProperty GetBindableProperty(Type elementType, string localName, IXmlLineInfo lineInfo,
309 bool throwOnError = false)
312 var bindableFieldInfo = elementType.GetFields().FirstOrDefault(fi => fi.Name == localName + "Property");
314 var bindableFieldInfo = elementType.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).FirstOrDefault(fi => fi.Name == localName + "Property");
316 if (null == bindableFieldInfo)
318 bindableFieldInfo = elementType.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy).FirstOrDefault(fi => fi.Name == localName + "Property");
321 Exception exception = null;
322 if (exception == null && bindableFieldInfo == null)
325 new XamlParseException(
326 Format("BindableProperty {0} not found on {1}", localName + "Property", elementType.Name), lineInfo);
329 if (exception == null)
330 return bindableFieldInfo.GetValue(null) as BindableProperty;
336 static object GetTargetProperty(object xamlelement, XmlName propertyName, HydrationContext context, IXmlLineInfo lineInfo)
338 var localName = propertyName.LocalName;
339 //If it's an attached BP, update elementType and propertyName
340 var bpOwnerType = xamlelement.GetType();
341 GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo);
342 var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false);
344 if (property != null)
347 var elementType = xamlelement.GetType();
348 var propertyInfo = elementType.GetRuntimeProperties().FirstOrDefault(p => p.Name == localName);
352 public static void SetPropertyValue(object xamlelement, XmlName propertyName, object value, object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo)
354 var localName = propertyName.LocalName;
355 var serviceProvider = new XamlServiceProvider(node, context);
356 Exception xpe = null;
357 var xKey = node is IElementNode && ((IElementNode)node).Properties.ContainsKey(XmlName.xKey) ? ((ValueNode)((IElementNode)node).Properties[XmlName.xKey]).Value as string : null;
359 //If it's an attached BP, update elementType and propertyName
360 var bpOwnerType = xamlelement.GetType();
361 var attached = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo);
363 var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false);
365 //If the target is an event, connect
366 if (xpe == null && TryConnectEvent(xamlelement, localName, attached, value, rootElement, lineInfo, out xpe))
369 //If Value is DynamicResource and it's a BP, SetDynamicResource
370 if (xpe == null && TrySetDynamicResource(xamlelement, property, value, lineInfo, out xpe))
373 //If value is BindingBase, SetBinding
374 if (xpe == null && TrySetBinding(xamlelement, property, localName, value, lineInfo, out xpe))
377 //Call TrySetProperty first and then TrySetValue to keep the code logic consistent whether it is through xaml or code.
378 //If we can assign that value to a normal property, let's do it
379 if (xpe == null && TrySetProperty(xamlelement, localName, value, lineInfo, serviceProvider, context, out xpe))
382 //If it's a BindableProberty, SetValue
383 if (xpe == null && TrySetValue(xamlelement, property, attached, value, lineInfo, serviceProvider, out xpe))
386 //If it's an already initialized property, add to it
387 if (xpe == null && TryAddToProperty(xamlelement, propertyName, value, xKey, lineInfo, serviceProvider, context, out xpe))
390 xpe = xpe ?? new XamlParseException($"Cannot assign property \"{localName}\": Property does not exist, or is not assignable, or mismatching type between value and property", lineInfo);
391 if (context.ExceptionHandler != null)
392 context.ExceptionHandler(xpe);
397 public static object GetPropertyValue(object xamlElement, XmlName propertyName, HydrationContext context, IXmlLineInfo lineInfo, out object targetProperty)
399 var localName = propertyName.LocalName;
400 Exception xpe = null;
402 targetProperty = null;
404 //If it's an attached BP, update elementType and propertyName
405 var bpOwnerType = xamlElement.GetType();
406 var attached = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo);
407 var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false);
409 //If it's a BindableProberty, GetValue
410 if (xpe == null && TryGetValue(xamlElement, property, attached, out value, lineInfo, out xpe, out targetProperty))
413 //If it's a normal property, get it
414 if (xpe == null && TryGetProperty(xamlElement, localName, out value, lineInfo, context, out xpe, out targetProperty))
417 xpe = xpe ?? new XamlParseException($"Property {localName} is not found or does not have an accessible getter", lineInfo);
418 if (context.ExceptionHandler != null)
419 context.ExceptionHandler(xpe);
426 static bool TryConnectEvent(object element, string localName, bool attached, object value, object rootElement, IXmlLineInfo lineInfo, out Exception exception)
433 var elementType = element.GetType();
434 var eventInfo = elementType.GetRuntimeEvent(localName);
435 var stringValue = value as string;
437 if (eventInfo == null || IsNullOrEmpty(stringValue))
440 var methodInfo = rootElement.GetType().GetRuntimeMethods().FirstOrDefault(mi => mi.Name == (string)value);
441 if (methodInfo == null)
443 exception = new XamlParseException($"No method {value} found on type {rootElement.GetType()}", lineInfo);
449 eventInfo.AddEventHandler(element, methodInfo.CreateDelegate(eventInfo.EventHandlerType, rootElement));
452 catch (ArgumentException ae)
454 exception = new XamlParseException($"Method {stringValue} does not have the correct signature", lineInfo, ae);
459 static bool TrySetDynamicResource(object element, BindableProperty property, object value, IXmlLineInfo lineInfo, out Exception exception)
463 var elementType = element.GetType();
464 var dynamicResource = value as DynamicResource;
465 var bindable = element as BindableObject;
467 if (dynamicResource == null || property == null)
470 if (bindable == null)
472 exception = new XamlParseException($"{elementType.Name} is not a BindableObject", lineInfo);
476 bindable.SetDynamicResource(property, dynamicResource.Key);
480 static bool TrySetBinding(object element, BindableProperty property, string localName, object value, IXmlLineInfo lineInfo, out Exception exception)
484 var elementType = element.GetType();
485 var binding = value.ConvertTo(typeof(BindingBase), pinfoRetriever: null, serviceProvider: null) as BindingBase;
486 var bindable = element as BindableObject;
491 if (bindable != null && property != null)
493 bindable.SetBinding(property, binding);
497 if (property != null)
498 exception = new XamlParseException($"{elementType.Name} is not a BindableObject or does not support native bindings", lineInfo);
503 static bool TrySetValue(object element, BindableProperty property, bool attached, object value, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, out Exception exception)
507 var elementType = element.GetType();
508 var bindable = element as BindableObject;
510 if (property == null)
513 if (serviceProvider != null && serviceProvider.IProvideValueTarget != null)
514 ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = property;
516 Func<MemberInfo> minforetriever;
518 minforetriever = () => property.DeclaringType.GetRuntimeMethod("Get" + property.PropertyName, new[] { typeof(BindableObject) });
521 minforetriever = () => property.DeclaringType.GetRuntimeProperties().LastOrDefault(p => p.Name == property.PropertyName);
523 //minforetriever = () => property.DeclaringType.GetRuntimeProperty(property.PropertyName);
524 var convertedValue = value.ConvertTo(property.ReturnType, minforetriever, serviceProvider);
526 if (bindable != null)
528 //SetValue doesn't throw on mismatching type, so check before to get a chance to try the property setting or the collection adding
529 var nullable = property.ReturnTypeInfo.IsGenericType &&
530 property.ReturnTypeInfo.GetGenericTypeDefinition() == typeof(Nullable<>);
531 if ((convertedValue == null && (!property.ReturnTypeInfo.IsValueType || nullable)) ||
532 (property.ReturnType.IsInstanceOfType(convertedValue)))
534 bindable.SetValue(property, convertedValue);
538 // This might be a collection; see if we can add to it
539 return TryAddValue(bindable, property, value, serviceProvider);
542 exception = new XamlParseException($"{elementType.Name} is not a BindableObject or does not support setting native BindableProperties", lineInfo);
546 static bool TryGetValue(object element, BindableProperty property, bool attached, out object value, IXmlLineInfo lineInfo, out Exception exception, out object targetProperty)
550 targetProperty = property;
551 var elementType = element.GetType();
552 var bindable = element as BindableObject;
554 if (property == null)
557 if (bindable == null)
560 value = bindable.GetValue(property);
564 static bool TrySetProperty(object element, string localName, object value, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, HydrationContext context, out Exception exception)
568 var elementType = element.GetType();
569 var propertyInfo = elementType.GetRuntimeProperties().FirstOrDefault(p => p.Name == localName);
571 if (propertyInfo == null || !propertyInfo.CanWrite || (setter = propertyInfo.SetMethod) == null)
574 if (!IsVisibleFrom(setter, context.RootElement))
577 if (serviceProvider != null && serviceProvider.IProvideValueTarget != null)
578 ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = propertyInfo;
580 object convertedValue = GetConvertedValue(propertyInfo.PropertyType, value, () => propertyInfo, serviceProvider);
582 if (null == convertedValue)
584 var methods = propertyInfo.PropertyType.GetMethods().Where(a => a.Name == "op_Implicit");
586 foreach (var method in methods)
588 var paramType = method.GetParameters()[0].ParameterType;
589 convertedValue = GetConvertedValue(paramType, value, () => propertyInfo, serviceProvider);
591 if (null != convertedValue)
593 var realValue = Activator.CreateInstance(propertyInfo.PropertyType);
594 convertedValue = method.Invoke(realValue, new object[] { convertedValue });
596 if (null != convertedValue)
604 if (null == convertedValue)
609 setter.Invoke(element, new object[] { convertedValue });
613 static private object GetConvertedValue(Type valueType, object value, Func<MemberInfo> minfoRetriever, XamlServiceProvider serviceProvider)
617 object convertedValue = value.ConvertTo(valueType, minfoRetriever, serviceProvider);
619 if (convertedValue != null && !valueType.IsInstanceOfType(convertedValue))
624 return convertedValue;
632 static bool TryGetProperty(object element, string localName, out object value, IXmlLineInfo lineInfo, HydrationContext context, out Exception exception, out object targetProperty)
636 var elementType = element.GetType();
637 PropertyInfo propertyInfo = null;
640 propertyInfo = elementType.GetRuntimeProperty(localName);
642 catch (AmbiguousMatchException)
644 // Get most derived instance of property
645 foreach (var property in elementType.GetRuntimeProperties().Where(prop => prop.Name == localName))
647 if (propertyInfo == null || propertyInfo.DeclaringType.IsAssignableFrom(property.DeclaringType))
648 propertyInfo = property;
652 targetProperty = propertyInfo;
653 if (propertyInfo == null || !propertyInfo.CanRead || (getter = propertyInfo.GetMethod) == null)
656 if (!IsVisibleFrom(getter, context.RootElement))
659 value = getter.Invoke(element, new object[] { });
663 static bool IsVisibleFrom(MethodInfo method, object rootElement)
667 if (method.IsPrivate && method.DeclaringType == rootElement.GetType())
669 if ((method.IsAssembly || method.IsFamilyOrAssembly) && method.DeclaringType.AssemblyQualifiedName == rootElement.GetType().AssemblyQualifiedName)
671 if (method.IsFamily && method.DeclaringType.IsAssignableFrom(rootElement.GetType()))
676 static bool TryAddToProperty(object element, XmlName propertyName, object value, string xKey, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, HydrationContext context, out Exception exception)
680 object targetProperty;
681 var collection = GetPropertyValue(element, propertyName, context, lineInfo, out targetProperty) as IEnumerable;
683 if (collection == null)
686 if (exception == null && TryAddToResourceDictionary(collection as ResourceDictionary, value, xKey, lineInfo, out exception))
689 if (exception != null)
692 var addMethod = collection.GetType().GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1);
693 if (addMethod == null)
696 if (serviceProvider != null && serviceProvider.IProvideValueTarget != null)
697 ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = targetProperty;
699 addMethod.Invoke(collection, new[] { value.ConvertTo(addMethod.GetParameters()[0].ParameterType, (Func<TypeConverter>)null, serviceProvider) });
703 static bool TryAddToResourceDictionary(ResourceDictionary resourceDictionary, object value, string xKey, IXmlLineInfo lineInfo, out Exception exception)
707 if (resourceDictionary == null)
711 resourceDictionary.Add(xKey, value);
712 else if (value is Tizen.NUI.Binding.Style)
713 resourceDictionary.Add((Tizen.NUI.Binding.Style)value);
714 else if (value is ResourceDictionary)
715 resourceDictionary.Add((ResourceDictionary)value);
716 else if (value is StyleSheets.StyleSheet)
717 resourceDictionary.Add((StyleSheets.StyleSheet)value);
720 exception = new XamlParseException("resources in ResourceDictionary require a x:Key attribute", lineInfo);
726 void SetTemplate(ElementTemplate dt, INode node)
728 #pragma warning disable 0612
729 ((IDataTemplate)dt).LoadTemplate = () =>
731 #pragma warning restore 0612
732 var cnode = node.Clone();
733 var context = new HydrationContext { ParentContext = Context, RootElement = Context.RootElement };
734 cnode.Accept(new XamlNodeVisitor((n, parent) => n.Parent = parent), node.Parent); //set parents for {StaticResource}
735 cnode.Accept(new ExpandMarkupsVisitor(context), null);
736 cnode.Accept(new NamescopingVisitor(context), null);
737 cnode.Accept(new CreateValuesVisitor(context), null);
738 cnode.Accept(new RegisterXNamesVisitor(context), null);
739 cnode.Accept(new FillResourceDictionariesVisitor(context), null);
740 cnode.Accept(new ApplyPropertiesVisitor(context, true), null);
741 return context.Values[cnode];
745 static bool TryAddValue(BindableObject bindable, BindableProperty property, object value, XamlServiceProvider serviceProvider)
747 if (property?.ReturnTypeInfo?.GenericTypeArguments == null)
752 if (property.ReturnType == null)
757 if (property.ReturnTypeInfo.GenericTypeArguments.Length != 1 ||
758 !property.ReturnTypeInfo.GenericTypeArguments[0].IsInstanceOfType(value))
761 // This might be a collection we can add to; see if we can find an Add method
762 var addMethod = GetAllRuntimeMethods(property.ReturnType)
763 .FirstOrDefault(mi => mi.Name == "Add" && mi.GetParameters().Length == 1);
764 if (addMethod == null)
767 // If there's an add method, get the collection
768 var collection = bindable.GetValue(property);
770 // And add the new value to it
771 addMethod.Invoke(collection, new[] { value.ConvertTo(addMethod.GetParameters()[0].ParameterType, (Func<TypeConverter>)null, serviceProvider) });
775 static IEnumerable<MethodInfo> GetAllRuntimeMethods(Type type)
777 return type.GetRuntimeMethods()
778 .Concat(type.GetTypeInfo().ImplementedInterfaces.SelectMany(t => t.GetRuntimeMethods()));
781 bool TrySetRuntimeName(XmlName propertyName, object source, object value, ValueNode node)
783 if (propertyName != XmlName.xName)
786 var runTimeName = source.GetType().GetTypeInfo().GetCustomAttribute<RuntimeNamePropertyAttribute>();
787 if (runTimeName == null)
790 SetPropertyValue(source, new XmlName("", runTimeName.Name), value, Context.RootElement, node, Context, node);
794 private PropertyInfo GetIndexer(object source, Type keyType, Type valueType) => source.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault(p => p.Name == "Item" && p.PropertyType.IsAssignableFrom(valueType) && p.GetIndexParameters().Length != 0 && p.GetIndexParameters()[0].ParameterType == keyType);