public static IElementFactory FromXamlElement(XamlElement element, Type targetType) { Type elementType = element.GetElementType(); if (elementType.GetDefaultConstructor() == null) { return FromElementFactory(FromXamlElementContent(element), targetType, element.Namespaces); } IElementInitializer elementInitializer = new ElementInitializer(element); IElementFactory elementFactory = new ElementFactory(elementType, elementInitializer); return FromElementFactory(elementFactory, targetType, element.Namespaces); }
public ElementCollectionContentInitializer(Type valueTargetType, IEnumerable <object> values) { elementsFactory = values.Select(value => ElementFactory.FromValue(value, valueTargetType, XamlNamespaces.Empty)).ToArray(); }
private static IElementFactory GetKeyDirectiveFactory(XamlElement element, Type keyType) { XamlMember keyDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.KeyDirective); return(keyDirective != null?ElementFactory.FromValue(keyDirective.GetSingleValue(), keyType, element.Namespaces) : null); }
private static IEnumerable <KeyValueElementFactory> CreateElementsFactories(Type dictionaryType, Type keyType, Type valueType, IEnumerable <object> values) { if (values.Any(value => !(value is XamlElement))) { throw new Granular.Exception("Can't add a value of type \"{0}\" to a dictionary, as it cannot have a key", values.First(value => !(value is XamlElement)).GetType().Name); } IEnumerable <XamlElement> valuesElements = System.Linq.Enumerable.Cast <XamlElement>(values); bool isValueProviderSupported = dictionaryType.GetCustomAttributes(true).OfType <SupportsValueProviderAttribute>().Any(); List <KeyValueElementFactory> list = new List <KeyValueElementFactory>(); foreach (XamlElement contentChild in valuesElements) { bool isShared = contentChild.Directives.All(directive => directive.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(directive.GetSingleValue(), typeof(bool), XamlNamespaces.Empty, null)); if (!isShared && !isValueProviderSupported) { throw new Granular.Exception($"Can't add a non shared value to \"{dictionaryType.FullName}\" as it does not declare a \"SupportsValueProvider\" attribute"); } IElementFactory contentChildFactory = isValueProviderSupported ? new DeferredValueFactory(contentChild, valueType, isShared) : ElementFactory.FromXamlElement(contentChild, valueType); list.Add(new KeyValueElementFactory(keyType, contentChildFactory, contentChild, isValueProviderSupported)); } return(list); }