C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了给IConfiguration写一个GetAppSetting扩展方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

IConfiguration一个 GetAppSetTing 扩展方法

Intro

在 .net core 中,微软已经认使用 appsetTings.json 来代替 app.config,并重新设计了一套完整的配置系统,可以支持 json/xml/ini/环境变量等。

在 .net core 中有一个 GetConnectionString 的扩展方法用来比较方便获取链接字符串,类似于在 .net framework 中使用 ConfigurationManager.ConnectionStrings["key"]获取链接字符串。

这次来参 GetConnectionString 实现一个 GetAppSetTing,类似于 .net framework 中使用 ConfigurationManager.AppSetTings获取配置值。

实现代码

GetConnectionString获取 ConnectionStrings 这个节点下的某个配置,
GetAppSetTing获取 AppSetTings 这个节点下的某个配置

实现代码

        /// <sumMary>
        /// GetAppSetTing
        /// Shorthand for getSection("AppSetTings")[key]
        /// </sumMary>
        /// <param name="configuration">IConfiguration instance</param>
        /// <param name="key">appSetTings key</param>
        /// <returns>app setTing value</returns>
        public static String GetAppSetTing([NotNull]this IConfiguration configuration,String key)
        {
            return configuration.GetSection("AppSetTings")[key];
        }

        /// <sumMary>
        /// GetAppSetTing
        /// Shorthand for getSection("AppSetTings")[key]
        /// </sumMary>
        /// <param name="configuration">IConfiguration instance</param>
        /// <param name="key">appSetTings key</param>
        /// <returns>app setTing value</returns>
        public static T GetAppSetTing<T>([NotNull]this IConfiguration configuration,String key)
        {
            return configuration.GetSection("AppSetTings")[key].To<T>();
        }

        /// <sumMary>
        /// GetAppSetTing
        /// Shorthand for getSection("AppSetTings")[key]
        /// </sumMary>
        /// <param name="configuration">IConfiguration instance</param>
        /// <param name="key">appSetTings key</param>
        /// <param name="DefaultValue">default value if not exist</param>
        /// <returns>app setTing value</returns>
        public static T GetAppSetTing<T>([NotNull] this IConfiguration configuration,String key,T DefaultValuE)
        {
            return configuration.GetSection("AppSetTings")[key].ToOrDefault(DefaultValuE);
        }

        /// <sumMary>
        /// GetAppSetTing
        /// Shorthand for getSection("AppSetTings")[key]
        /// </sumMary>
        /// <param name="configuration">IConfiguration instance</param>
        /// <param name="key">appSetTings key</param>
        /// <param name="DefaultValueFunc">default value func if not exist to get a default value</param>
        /// <returns>app setTing value</returns>
        public static T GetAppSetTing<T>([NotNull] this IConfiguration configuration,Func<T> DefaultValueFunC)
        {
            return configuration.GetSection("AppSetTings")[key].ToOrDefault(DefaultValueFunc);
        }

查看源码

使用

使用起来和 GetConnectionString 差不多

测试 appsetTings.json

{
  "ConnectionStrings": {
    "TestDb": "server=.;database=Test;uid=weihanli;pwd=Admin888"
  },"AppSetTings":{
    "number": 12,"City": "Shanghai"
  }
}

GetAppSetTing 示例

        IConfiguration configuration = new ConfigurationBuilder()
            // ...
            .AddJsonFile("appsetTings.json")
            .build();

        var city = configuration.GetAppSetTing("City");
        var number = configuration.GetAppSetTing<int>("number");
        System.Console.WriteLine($"City:{City},number:{number}");
@H_482_63@memo

你可以复制上面的代码在你自己的代码里使用,也可以直接使用 WeihanLi.Common 这一 nuget 包

大佬总结

以上是大佬教程为你收集整理的给IConfiguration写一个GetAppSetting扩展方法全部内容,希望文章能够帮你解决给IConfiguration写一个GetAppSetting扩展方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: