/// <summary> /// Creates an element of the given type or returns null, if there is /// no such type. /// </summary> /// <typeparam name="TElement">The type of element to create.</typeparam> /// <param name="document">The responsible document.</param> /// <returns>The new element, if available.</returns> public static TElement CreateElement <TElement>(this IDocument document) where TElement : IElement { var type = typeof(ApiExtensions).GetAssembly().GetTypes() .Where(m => m.Implements <TElement>()) .FirstOrDefault(m => !m.IsAbstractClass()); if (type == null) { return(default(TElement)); } var ctor = type.GetConstructor(); if (ctor == null) { return(default(TElement)); } var element = (TElement)ctor.Invoke(null); var el = element as Element; if (element != null) { document.Adopt(element); } if (el != null) { el.Close(); } return(element); }
/// <summary> /// Creates an element of the given type or throws an exception, if /// there is no such type. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="document">The responsible document.</param> /// <returns>The created element.</returns> public static TElement CreateElement <TElement>(this IDocument document) where TElement : IElement { if (document == null) { throw new ArgumentNullException("document"); } var type = typeof(ApiExtensions).GetAssembly().GetTypes() .Where(m => m.Implements <TElement>()) .FirstOrDefault(m => !m.IsAbstractClass()); if (type != null) { var ctors = type.GetConstructors() .OrderBy(m => m.GetParameters().Length); foreach (var ctor in ctors) { var parameters = ctor.GetParameters(); var arguments = new Object[parameters.Length]; for (var i = 0; i < parameters.Length; i++) { var isDocument = parameters[i].ParameterType == typeof(Document); arguments[i] = isDocument ? document : parameters[i].DefaultValue; } var obj = ctor.Invoke(arguments); if (obj != null) { var element = (TElement)obj; var baseElement = element as Element; if (baseElement != null) { baseElement.SetupElement(); } document.Adopt(element); return(element); } } } throw new ArgumentException("No element could be created for the provided interface."); }
/// <summary> /// Creates an element of the given type or returns null, if there is /// no such type. /// </summary> /// <typeparam name="TElement">The type of element to create.</typeparam> /// <param name="document">The responsible document.</param> /// <returns>The new element, if available.</returns> public static TElement CreateElement <TElement>(this IDocument document) where TElement : IElement { if (document == null) { throw new ArgumentNullException("document"); } var type = typeof(ApiExtensions).GetAssembly().GetTypes() .Where(m => m.Implements <TElement>()) .FirstOrDefault(m => !m.IsAbstractClass()); if (type == null) { return(default(TElement)); } var ctor = type.GetConstructor(); var parameterLess = ctor != null; if (parameterLess == false) { ctor = type.GetConstructor(new Type[] { typeof(Document) }); } if (ctor == null) { return(default(TElement)); } var element = (TElement)(parameterLess ? ctor.Invoke(null) : ctor.Invoke(new Object[] { document })); var el = element as Element; if (element != null) { document.Adopt(element); } return(element); }