/// <summary> /// Loads a new instance of the specified type from a section in a .config file. /// </summary> /// <typeparam name="T">The type into which the .config XML should be deserialized.</typeparam> /// <param name="sectionName">The .config file section name</param> /// <param name="shouldThrowExceptionIfSectionNotFound"> /// Should an exception be thrown if the section is not found? (default = <c>true</c>). /// </param> /// <returns>New instance of the specified type.</returns> /// <exception cref="ConfigurationErrorsException"> if /// <para>exception is thrown by <see cref="ConfigurationManager.GetSection(string)"/></para> /// <para>* unable to cast .config section to <typeparamref name="T"/>.</para> /// </exception> /// <example> /// <para>.config XML:</para> /// <code><![CDATA[ /// <configSections> /// ... /// <section name="fooConfiguration" type="SparkyTools.XmlConfig.ConfigurationSectionDeserializer, SparkyTools.XmlConfig" /> /// ... /// </configSections> /// ... /// <fooConfiguration type="FooNamespace.Foo, FooAssemblyName"> /// <Bar>bar value</Bar> /// <Baz>baz value</Baz> /// <Qux>qux value</Qux> /// </fooConfiguration> /// ]]></code> /// <para>C# code to load instance from .config file:</para> /// <code><![CDATA[ /// Foo foo = ConfigurationSectionDeserializer.Load<Foo>("fooConfiguration"); /// ]]></code> /// </example> public static T Load <T>(string sectionName, bool shouldThrowExceptionIfSectionNotFound = true) { try { return((T)ConfigurationSectionDeserializerHelper.GetSection( sectionName, shouldThrowExceptionIfSectionNotFound)); } catch (InvalidCastException ex) { throw ConfigurationSectionDeserializerHelper.NewConfigurationErrorsException(ex, sectionName); } }
/// <summary> /// Loads a new <see cref="IList{T}"/> from a section in a .config file. /// </summary> /// <typeparam name="T">The types into which the .config XML should be deserialized.</typeparam> /// <param name="sectionName">The .config file section name</param> /// <param name="shouldThrowExceptionIfSectionNotFound"> /// Should an exception be thrown if the section is not found? (default = <c>true</c>). /// </param> /// <param name="shouldAllowEmptyList"> /// Should an empty list be allowed? (default = <c>false</c>.) /// </param> /// <returns>New <see cref="IList{T}"/> instance.</returns> /// <exception cref="ConfigurationErrorsException"> if /// <para>exception is thrown by <see cref="ConfigurationManager.GetSection(string)"/></para> /// <para>* unable to cast .config section to <see cref="IList{T}"/>.</para> /// <para>* <paramref name="shouldAllowEmptyList"/> is <c>false</c> and the list is empty.</para> /// </exception> /// <example> /// <para>.config XML:</para> /// <code><![CDATA[ /// <configSections> /// ... /// <section name="foos" type="SparkyTools.XmlConfig.ConfigurationSectionListDeserializer, SparkyTools.XmlConfig" /> /// ... /// </configSections> /// ... /// <foos type="FooNamespace.Foo, FooAssemblyName"> /// <Foo> /// <Bar>bar 1</Bar> /// <Baz>baz1 value</Baz> /// <Qux>qux1 value</Qux> /// </Foo> /// <Foo> /// <Bar>bar1 value</Bar> /// <Baz>baz1 value</Baz> /// <Qux>qux1 value</Qux> /// </Foo> /// <Foo> /// <Bar>bar1 value</Bar> /// <Baz>baz1 value</Baz> /// <Qux>qux1 value</Qux> /// </Foo> /// </foos> /// ]]></code> /// <para>C# code to load instance from .config file:</para> /// <code><![CDATA[ /// Foo foo = ConfigurationSectionDeserializer.Load<Foo>("fooConfiguration"); /// ]]></code> /// </example> public static IList <T> Load <T>( string sectionName, bool shouldThrowExceptionIfSectionNotFound = true, bool shouldAllowEmptyList = false) { var list = ConfigurationSectionDeserializerHelper.GetSection( sectionName, shouldThrowExceptionIfSectionNotFound) as IList <T>; if (list?.Count == 0 && !shouldAllowEmptyList) { throw ConfigurationSectionDeserializerHelper.NewConfigurationErrorsException(sectionName, "Item count = zero."); } return(list); }