/// <summary>
        /// Gets a section from this configuration source.
        /// </summary>
        /// <typeparam name="T">Type of the section that should be looked up.</typeparam>
        /// <returns>The section as it is configured in this source.</returns>
        public T GetSection <T>()
        {
            var sectionName = ConfigurationSectionAttribute.GetSectionName <T>();

            if (!_root.ContainsKey(sectionName))
            {
                return(default(T));
            }

            try
            {
                var serialized = JsonConvert.SerializeObject(_root[sectionName]);
                return(JsonConvert.DeserializeObject <T>(serialized));
            }
            catch (Exception exception)
            {
                throw new ConfigurationException($"Could not read configuration section {sectionName}. ", exception);
            }
        }
 public void ConfigurationSectionAttribute_GetSectionNameReturnsSectionNameOfType()
 {
     Assert.Equal("section", ConfigurationSectionAttribute
                  .GetSectionName <TypeWithConfigurationSectionAttribute>());
 }
 public void ConfigurationSectionAttribute_GetSectionNameThrowsInvalidOperationExceptionForTypeWithoutAttribute()
 {
     Assert.Throws <InvalidOperationException>(() =>
                                               ConfigurationSectionAttribute.GetSectionName <TypeWithoutConfigurationSectionAttribute>());
 }