Class HierarchyDecoratorExtensions
Extension methods for the IHierarchy<T> interface hidden behind the IDecorator<T> interface.
public static class HierarchyDecoratorExtensions
- Inheritance
-
HierarchyDecoratorExtensions
Examples
HierarchyDecoratorExtensions provides extension methods on Decorator.Enclose for inspecting XML serialization metadata on IHierarchy<object> nodes. This example creates a Hierarchy<object> tree with a Person node (annotated with [XmlRoot("person")]) and an Address child node, then uses decorator methods to call GetXmlQualifiedEntity, TryGetXmlRootAttribute, TryGetXmlElementAttribute, TryGetXmlTextAttribute, TryGetXmlAttributeAttribute, IsNodeEnumerable, HasXmlIgnoreAttribute, and OrderByXmlAttributes. Console output displays the qualified entity local name, boolean results for attribute detection, and the count of ordered nodes.
using System;
using Cuemon;
using Cuemon.Extensions.Runtime;
using Cuemon.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
namespace MyApp.Examples;
public static class HierarchyDecoratorExtensionsExample
{
public static void Demonstrate()
{
var hierarchy = new Hierarchy<object>();
var root = hierarchy.Add(new Person { Name = "Alice" });
var child = root.Add(new Address { City = "Paris" });
var decorator = Decorator.Enclose((IHierarchy<object>)child);
var rootDecorator = Decorator.Enclose((IHierarchy<object>)root);
var qualifiedEntity = decorator.GetXmlQualifiedEntity();
var hasRoot = rootDecorator.TryGetXmlRootAttribute(out _);
var hasElement = decorator.TryGetXmlElementAttribute(out _);
var hasText = decorator.TryGetXmlTextAttribute(out _);
var hasAttribute = decorator.TryGetXmlAttributeAttribute(out _);
var ordered = Decorator.Enclose((IEnumerable<IHierarchy<object>>)new[] { root, child }).OrderByXmlAttributes<object>().ToList();
Console.WriteLine(qualifiedEntity.LocalName);
Console.WriteLine(hasRoot);
Console.WriteLine(hasElement || hasText || hasAttribute);
Console.WriteLine(decorator.IsNodeEnumerable());
Console.WriteLine(decorator.HasXmlIgnoreAttribute());
Console.WriteLine(ordered.Count);
}
[XmlRoot("person")]
private sealed class Person
{
public string Name { get; set; }
}
private sealed class Address
{
public string City { get; set; }
}
}
Methods
GetXmlQualifiedEntity(IDecorator<IHierarchy<object>>, XmlQualifiedEntity)
Resolves an XmlQualifiedEntity from either the specified qualifiedEntity or from the underlying IHierarchy{object} of the decorator.
public static XmlQualifiedEntity GetXmlQualifiedEntity(this IDecorator<IHierarchy<object>> decorator, XmlQualifiedEntity qualifiedEntity = null)
Parameters
decoratorIDecorator<IHierarchy<object>>The IDecorator{IHierarchy{object}} to extend.
qualifiedEntityXmlQualifiedEntityThe optional XmlQualifiedEntity that is part of the equation.
Returns
- XmlQualifiedEntity
An XmlQualifiedEntity that is either from
qualifiedEntity, embedded withindecorator, XmlRootAttribute, XmlElementAttribute, XmlAttributeAttribute or resolved from either member name or member type (in that order).
Exceptions
- ArgumentNullException
decoratorcannot be null.
HasXmlIgnoreAttribute(IDecorator<IHierarchy<object>>)
Determines whether the underlying IHierarchy{object} of the decorator implements XmlIgnoreAttribute.
public static bool HasXmlIgnoreAttribute(this IDecorator<IHierarchy<object>> decorator)
Parameters
decoratorIDecorator<IHierarchy<object>>The IDecorator{IHierarchy{object}} to extend.
Returns
- bool
trueif the underlying IHierarchy{object} of thedecoratorimplements XmlIgnoreAttribute; otherwise,false.
Exceptions
- ArgumentNullException
decoratorcannot be null.
IsNodeEnumerable(IDecorator<IHierarchy<object>>)
Determines whether the underlying IHierarchy{object} of the decorator implements either IEnumerable or IEnumerable<T> and is not a string.
public static bool IsNodeEnumerable(this IDecorator<IHierarchy<object>> decorator)
Parameters
decoratorIDecorator<IHierarchy<object>>The IDecorator{IHierarchy{object}} to extend.
Returns
- bool
trueif the underlying IHierarchy{object} of thedecoratorimplements either IEnumerable or IEnumerable<T> and is not a string; otherwise,false.
Exceptions
- ArgumentNullException
decoratorcannot be null.
OrderByXmlAttributes<T>(IDecorator<IEnumerable<IHierarchy<T>>>)
Orders a sequence of IHierarchy<T> from the underlying IEnumerable{IHierarchy{object}} of the decorator by nodes having an XmlAttributeAttribute decoration.
public static IEnumerable<IHierarchy<T>> OrderByXmlAttributes<T>(this IDecorator<IEnumerable<IHierarchy<T>>> decorator)
Parameters
decoratorIDecorator<IEnumerable<IHierarchy<T>>>The IDecorator{IEnumerable{IHierarchy{object}}} to extend.
Returns
- IEnumerable<IHierarchy<T>>
A sequence of IHierarchy<T> that is sorted by nodes having an XmlAttributeAttribute decoration first.
Type Parameters
TThe type of the node represented in the hierarchical structure.
Exceptions
- ArgumentNullException
decoratorcannot be null.
TryGetXmlAttributeAttribute(IDecorator<IHierarchy<object>>, out XmlAttributeAttribute)
Attempts to get an XmlAttributeAttribute from the underlying IHierarchy{object} of the decorator.
public static bool TryGetXmlAttributeAttribute(this IDecorator<IHierarchy<object>> decorator, out XmlAttributeAttribute xmlAttribute)
Parameters
decoratorIDecorator<IHierarchy<object>>The IDecorator{IHierarchy{object}} to extend.
xmlAttributeXmlAttributeAttributeWhen this method returns, contains the XmlAttributeAttribute associated with the underlying IHierarchy{object} of the
decorator.
Returns
- bool
trueif underlying IHierarchy{object} of thedecoratorcontains an XmlAttributeAttribute,falseotherwise.
TryGetXmlElementAttribute(IDecorator<IHierarchy<object>>, out XmlElementAttribute)
Attempts to get an XmlElementAttribute from the underlying IHierarchy{object} of the decorator.
public static bool TryGetXmlElementAttribute(this IDecorator<IHierarchy<object>> decorator, out XmlElementAttribute xmlAttribute)
Parameters
decoratorIDecorator<IHierarchy<object>>The IDecorator{IHierarchy{object}} to extend.
xmlAttributeXmlElementAttributeWhen this method returns, contains the XmlElementAttribute associated with the underlying IHierarchy{object} of the
decorator.
Returns
- bool
trueif underlying IHierarchy{object} of thedecoratorcontains an XmlElementAttribute,falseotherwise.
TryGetXmlRootAttribute(IDecorator<IHierarchy<object>>, out XmlRootAttribute)
Attempts to get an XmlRootAttribute from the underlying IHierarchy{object} of the decorator.
public static bool TryGetXmlRootAttribute(this IDecorator<IHierarchy<object>> decorator, out XmlRootAttribute xmlAttribute)
Parameters
decoratorIDecorator<IHierarchy<object>>The IDecorator{IHierarchy{object}} to extend.
xmlAttributeXmlRootAttributeWhen this method returns, contains the XmlRootAttribute associated with the underlying IHierarchy{object} of the
decorator.
Returns
- bool
trueif underlying IHierarchy{object} of thedecoratorcontains an XmlRootAttribute,falseotherwise.
TryGetXmlTextAttribute(IDecorator<IHierarchy<object>>, out XmlTextAttribute)
Attempts to get an XmlTextAttribute from the underlying IHierarchy{object} of the decorator.
public static bool TryGetXmlTextAttribute(this IDecorator<IHierarchy<object>> decorator, out XmlTextAttribute xmlAttribute)
Parameters
decoratorIDecorator<IHierarchy<object>>The IDecorator{IHierarchy{object}} to extend.
xmlAttributeXmlTextAttributeWhen this method returns, contains the XmlTextAttribute associated with the underlying IHierarchy{object} of the
decorator.
Returns
- bool
trueif underlying IHierarchy{object} of thedecoratorcontains an XmlTextAttribute,falseotherwise.