private static IElementFactory GetDeferredKeyFactory(XamlElement xamlElement) { Type elementType = xamlElement.GetElementType(); IDeferredValueKeyProvider provider = DeferredValueKeyProviders.GetDeferredValueKeyProvider(elementType); if (provider != null) { return(new DeferredKeyFactory(provider, xamlElement)); } string keyPropertyName = DictionaryKeyPropertyAttribute.GetPropertyName(elementType); if (!keyPropertyName.IsNullOrWhiteSpace()) { XamlMember keyMember = xamlElement.Members.FirstOrDefault(member => member.Name.LocalName == keyPropertyName); if (keyMember != null) { IPropertyAdapter keyProperty = PropertyAdapter.CreateAdapter(elementType, keyPropertyName); return(ElementFactory.FromValue(keyMember.Values.Single(), keyProperty.PropertyType, xamlElement.Namespaces, xamlElement.SourceUri)); } } return(null); }
public ElementInitializer(XamlElement element) { elementType = element.GetElementType(); namespaces = element.Namespaces; memberInitializers = CreateMemberInitializers(element); contentInitializer = CreateContentInitializer(element); nameDirectiveValue = GetNameDirectiveValue(element); nameProperty = GetNameProperty(element.GetElementType()); }
private XamlElement Parse() { this.tokens = new ReadOnlyStack <Token>(lexer.GetTokens(text)); XamlElement root = MatchElement(); if (!tokens.IsEmpty) { throw new Granular.Exception("Can't parse \"{0}\", end of stream is expected at index {1}", text, tokens.Peek().Start); } return(root); }
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)); }
private static IElementFactory FromXamlElementContent(XamlElement element) { if (element.Members.Any()) { throw new Granular.Exception("Element \"{0}\" can't have members, as it's not a collection type and does not declare ContentProperty and can only be converted from its content", element.Name); } if (element.Values.Count() > 1) { throw new Granular.Exception("Element \"{0}\" can't have multiple children, as it's not a collection type and does not declare ContentProperty and can only be converted from its content", element.Name); } return(FromValue(element.Values.First(), element.GetElementType(), element.Namespaces, element.SourceUri)); }
public static IElementFactory FromXamlElement(XamlElement element, Type targetType) { Type elementType = element.GetElementType(); if (element.Values.Any() && ContentPropertyAttribute.GetPropertyName(elementType).IsNullOrEmpty() && !ElementCollectionContentInitailizer.IsCollectionType(elementType)) { return(FromElementFactory(FromXamlElementContent(element), targetType, element.Namespaces, element.SourceUri)); } IElementInitializer elementInitializer = new ElementInitializer(element); IElementFactory elementFactory = new ElementFactory(elementType, elementInitializer); return(FromElementFactory(elementFactory, targetType, element.Namespaces, element.SourceUri)); }
private static IEnumerable <IElementInitializer> CreateMemberInitializers(XamlElement element, Type elementType) { List <IElementInitializer> list = new List <IElementInitializer>(); int index = 0; foreach (XamlMember member in element.Members) { // markup extensions may contain members with an empty name, the name should be resolved from the member index XamlName memberName = member.Name.IsEmpty ? GetParameterName(elementType, index) : member.Name; list.Add(ElementMemberInitializer.Create(memberName.ResolveContainingType(elementType), memberName.MemberName, member.Values, member.Namespaces, member.SourceUri)); index++; } return(list); }
private static IElementInitializer CreateContentInitializer(XamlElement element) { Type elementType = element.GetElementType(); string contentPropertyName = PropertyAttribute.GetPropertyName <ContentPropertyAttribute>(elementType); if (!contentPropertyName.IsNullOrEmpty()) { return(ElementMemberInitializer.Create(new XamlName(contentPropertyName), elementType, element.Values, element.Namespaces)); } if (ElementCollectionContentInitailizer.IsCollectionType(elementType)) { return(ElementCollectionContentInitailizer.Create(element.Values, elementType)); } return(null); }
private static IElementFactory FromXamlElementContent(XamlElement element) { if (element.Members.Any()) { throw new Granular.Exception("Element \"{0}\" can't have members, as its type doesn't have a default constructor and it can only be converted from its content", element.Name); } if (!element.Values.Any()) { throw new Granular.Exception("Element \"{0}\" must have a value, as its type doesn't have a default constructor and it can only be converted from its content", element.Name); } if (element.Values.Count() > 1) { throw new Granular.Exception("Element \"{0}\" can't have multiple children, as its type doesn't have a default constructor and it can only be converted from its content", element.Name); } return(FromValue(element.Values.First(), element.GetElementType(), element.Namespaces)); }
private static IElementInitializer CreateContentInitializer(XamlElement element, Type elementType) { if (!element.Values.Any()) { return(null); } string contentPropertyName = ContentPropertyAttribute.GetPropertyName(elementType); if (!contentPropertyName.IsNullOrEmpty()) { return(ElementMemberInitializer.Create(elementType, contentPropertyName, element.Values, element.Namespaces, element.SourceUri)); } if (ElementCollectionContentInitailizer.IsCollectionType(elementType)) { return(ElementCollectionContentInitailizer.Create(element.Values, elementType)); } throw new Granular.Exception("Cannot add content to element of type \"{0}\" as it's not a collection type and does not declare ContentProperty", elementType.Name); }
public static void Load(object target, XamlElement resource) { IElementInitializer initializer = new ElementInitializer(resource); initializer.InitializeElement(target, new InitializeContext()); }
public static object Load(XamlElement resource) { IElementFactory factory = ElementFactory.FromXamlElement(resource, null); return(factory.CreateElement(new InitializeContext())); }
public static Type GetElementType(this XamlElement element) { return(TypeParser.ParseType(element.Name)); }
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); }
public KeyValueElementFactory(Type keyType, IElementFactory valueFactory, XamlElement xamlElement) { this.valueFactory = valueFactory; keyProperty = GetKeyProperty(valueFactory.ElementType); keyDirectiveFactory = GetKeyDirectiveFactory(xamlElement, keyType); if (keyDirectiveFactory == null && keyProperty == null) { throw new Granular.Exception("Dictionary item \"{0}\" must have a key", xamlElement.Name); } }
private static string GetNameDirectiveValue(XamlElement element) { XamlMember nameDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.NameDirective); return(nameDirective != null ? (string)nameDirective.GetSingleValue() : null); }
public DeferredKeyFactory(IDeferredValueKeyProvider provider, XamlElement element) { this.provider = provider; this.element = element; }
public DeferredValueFactory(XamlElement element, Type targetType, bool isShared) { this.element = element; this.targetType = targetType; this.isShared = isShared; }