/// <summary> /// Populate configuration from XML via the specified <see cref="XmlReader" />. /// </summary> /// <param name="reader"><see cref="XmlReader" /> stream from the configuration file.</param> /// <seealso cref="NChronicle.ConfigureFrom(string, bool, int)"/> public void ReadXml(XmlReader reader) { if (reader.Name != nameof(ChronicleConfiguration)) { throw new XmlException($"Unexpected node '{reader.Name}', expected '{nameof(ChronicleConfiguration)}'."); } while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case nameof(this.Libraries): while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "Library": var typeStr = reader.GetAttribute("Type"); if (string.IsNullOrEmpty(typeStr)) { throw new XmlException ("Unexpected library configuration, type is missing."); } var type = Type.GetType(typeStr, false, true); if (type == null) { throw new TypeLoadException ($"Unexpected library configuration, type {typeStr} could not be found."); } #if NETFX if (!typeof(IChronicleLibrary).IsAssignableFrom(type)) #else if (!typeof(IChronicleLibrary).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) #endif { throw new TypeLoadException ($"Unexpected library configuration, type {type.Name} does not implement {nameof(IChronicleLibrary)}."); } IChronicleLibrary library = null; try { library = Activator.CreateInstance(type) as IChronicleLibrary; } catch (MissingMethodException e) { throw new TypeLoadException ($"Unexpected library configuration for {type.Name}, type does not define a public parameterless constructor.", e); } if (library == null) { throw new TypeLoadException ($"Unexpected library configuration for {type.Name}, instance could not be cast to {nameof(IChronicleLibrary)}."); } library.ReadXml(reader); this.Libraries.Add(library); break; default: reader.Skip(); break; } } else if (reader.NodeType == XmlNodeType.EndElement) { break; } } break; default: reader.Skip(); break; } } else if (reader.NodeType == XmlNodeType.EndElement) { break; } } }
/// <summary> /// Add an <see cref="IChronicleLibrary"/> instance to this configuration. /// </summary> /// <param name="library">The configured <see cref="IChronicleLibrary"/>.</param> public void WithLibrary(IChronicleLibrary library) { this.Libraries.Add(library); }