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 IEnumerable<IElementInitializer> CreateMemberInitializers(XamlElement element) { Type elementType = element.GetElementType(); 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, elementType, member.Values, member.Namespaces)); index++; } return list; }
public ElementInitializer(XamlElement element) { elementType = element.GetElementType(); namespaces = element.Namespaces; memberInitializers = CreateMemberInitializers(element); contentInitializer = CreateContentInitializer(element); nameDirectiveValue = GetNameDirectiveValue(element); nameProperty = GetNameProperty(element.GetElementType()); }
private static IEnumerable<IElementInitializer> CreateMemberInitializers(XamlElement element) { Type elementType = element.GetElementType(); List<IElementInitializer> list = new List<IElementInitializer>(); int index = 0; foreach (XamlAttribute attribute in element.GetMemberAttributes()) { // markup extensions can contain empty attributes, the member name is determined by its index XamlName memberName = attribute.Name.IsEmpty ? GetParameterName(elementType, index) : attribute.Name; list.Add(ElementMemberInitializer.FromXamlAttribute(memberName, attribute, elementType)); index++; } foreach (XamlElement child in element.GetMemberChildren()) { list.Add(ElementMemberInitializer.FromXamlElement(child.Name, child, elementType)); } return list; }
private static IElementFactory FromXamlElementContent(XamlElement element) { if (element.GetMemberNodes().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); } IEnumerable<XamlElement> contentChilren = element.GetContentChildren(); if (contentChilren.Any() && !element.TextValue.IsNullOrEmpty()) { throw new Granular.Exception("Element \"{0}\" cannot have both children and text value", element.Name); } if (!contentChilren.Any()) { return new ConvertedElementFactory(new ConstantElementFactory(element.TextValue), element.GetElementType(), element.Namespaces); } if (contentChilren.Count() == 1) { return ElementFactory.FromXamlElement(contentChilren.First(), element.GetElementType()); } 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); }
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 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); }