123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using EasyDevCore.Common;
- using EasyDevCore.Configuration.Configuration;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace EasyDevCore.Configuration
- {
- /// <summary>
- ///
- /// </summary>
- public static class ConfigurationHelper
- {
- /// <summary>
- /// Adds the constant variables (constant declare in "AppSettings:Constants" section).
- /// </summary>
- /// <param name="builder">The builder.</param>
- /// <param name="configurationRoot">The configuration root.</param>
- /// <returns></returns>
- public static IConfigurationBuilder AddConstantVariables(this IConfigurationBuilder builder, IConfigurationRoot configurationRoot = null)
- {
- builder.Add(new SubstitutingConfigurationSource(configurationRoot ?? builder.Build(), "AppSettings:Constants"));
- return builder;
- }
- /// <summary>
- /// Adds the decrypting data.
- /// </summary>
- /// <param name="builder">The builder.</param>
- /// <param name="decryptFunc">The decrypt function.</param>
- /// <param name="configurationRoot">The configuration root.</param>
- /// <returns></returns>
- public static IConfigurationBuilder AddDecryptingData(this IConfigurationBuilder builder, Func<string, string, string> decryptFunc, IConfigurationRoot configurationRoot = null)
- {
- builder.Add(new ProtectDataConfigurationSource(configurationRoot ?? builder.Build(), encryptFunc: null, decryptFunc));
- return builder;
- }
- /// <summary>
- /// Adds the protect data.
- /// </summary>
- /// <param name="builder">The builder.</param>
- /// <param name="encryptFunc">The encrypt function.</param>
- /// <param name="decryptFunc">The decrypt function.</param>
- /// <param name="configurationRoot">The configuration root.</param>
- /// <returns></returns>
- public static IConfigurationBuilder AddProtectData(this IConfigurationBuilder builder, Func<string, string, string> encryptFunc, Func<string, string, string> decryptFunc, IConfigurationRoot configurationRoot = null)
- {
- builder.Add(new ProtectDataConfigurationSource(configurationRoot ?? builder.Build(), encryptFunc, decryptFunc));
- return builder;
- }
- /// <summary>
- /// Gets the configuation root.
- /// </summary>
- /// <param name="section">The section.</param>
- /// <returns></returns>
- public static IConfigurationRoot GetConfiguationRoot(this IConfiguration section)
- {
- if (section is IConfigurationRoot)
- {
- return (IConfigurationRoot)section;
- }
- else
- {
- var fieldInfo = section.GetType().GetField("_root", BindingFlags.NonPublic | BindingFlags.Instance);
- var root = fieldInfo.GetValue(section);
- return (IConfigurationRoot)root;
- }
- }
- /// <summary>
- /// Binds the specified section.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="section">The section.</param>
- /// <returns></returns>
- /// <exception cref="ConfigurationErrorsException">
- /// </exception>
- public static T Bind<T>(this IConfigurationSection section)
- {
- try
- {
- if (section.Value != null)
- return section.Value.ConvertTo<T>();
- if (typeof(T).HasInterface<IEnumerable>()
- && !typeof(T).HasInterface<IDictionary>())
- {
- var values = section.GetChildren().Map(GetValue);
- return values.ConvertTo<T>();
- }
- }
- catch (Exception ex)
- {
- var message = $"The {section.Key} setting had an invalid format. " +
- $"The value \"{section.Value}\" could not be cast to type {typeof(T).FullName}";
- throw new ConfigurationErrorsException(message, ex);
- }
- try
- {
- var to = typeof(T).CreateInstance();
- section.Bind(to);
- return (T)to;
- }
- catch (InvalidOperationException ex)
- {
- throw new ConfigurationErrorsException(ex.Message, ex);
- }
- }
- /// <summary>
- /// Binds the specified section name.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="config">The configuration.</param>
- /// <param name="sectionName">Name of the section.</param>
- /// <returns></returns>
- public static T Bind<T>(this IConfiguration config, string sectionName = null) => Bind<T>(config.GetSection(sectionName ?? typeof(T).Name));
- /// <summary>
- /// Existses the specified key.
- /// </summary>
- /// <param name="section">The section.</param>
- /// <param name="key">The key.</param>
- /// <returns></returns>
- public static bool Exists(this IConfigurationSection section, string key) => section.GetChildren().Any(x => x.Key == key) || section.GetSection(key).Exists();
- /// <summary>
- /// Existses the specified key.
- /// </summary>
- /// <param name="config">The configuration.</param>
- /// <param name="key">The key.</param>
- /// <returns></returns>
- public static bool Exists(this IConfiguration config, string key) => config.GetChildren().Any(x => x.Key == key) || config.GetSection(key).Exists();
- private static void GetAllChildren(List<IConfigurationSection> list, IConfiguration config)
- {
- foreach(var item in config.GetChildren())
- {
- if (item is IConfigurationSection) list.Add(item);
- GetAllChildren(list, item);
- }
- }
- /// <summary>
- /// Gets all children.
- /// </summary>
- /// <param name="configuration">The configuration.</param>
- /// <returns></returns>
- public static IEnumerable<IConfigurationSection> GetAllChildren(this IConfiguration configuration)
- {
- List<IConfigurationSection> list = new();
- GetAllChildren(list, configuration);
- return list;
- }
- /// <summary>Gets the value.</summary>
- /// <param name="section">The section.</param>
- /// <returns>
- /// <br />
- /// </returns>
- static object GetValue(this IConfigurationSection section)
- {
- if (section == null)
- return null;
- if (section.Value != null)
- return section.Value;
- var children = section.GetChildren();
- var first = children.FirstOrDefault();
- if (first == null)
- return null;
- if (first.Key == "0")
- {
- var to = children.Select(GetValue).ToList();
- if (to.Count > 0)
- return to;
- }
- else
- {
- var to = new Dictionary<string, object>();
- foreach (var child in children)
- {
- to[child.Key] = GetValue(child);
- }
- if (to.Count > 0)
- return to;
- }
- return null;
- }
- }
- }
|