zoukankan      html  css  js  c++  java
  • .NET Core 配置

    下面的代码演示了如何在.NET Core中使用“内存配置”:

        /// <summary>
        /// 获取格式设置
        /// </summary>
        /// <returns></returns>
        public static FormatSettings GetFormatSettings()
        {
          // Key使用冒号:分隔路径,实现层级化
          Dictionary<string, string> initialData = new Dictionary<string, string>
          {
            // 时间格式化
            ["Format:DateTime:LongDatePattern"] = "dddd, MMMM d, yyyy",
            ["Format:DateTime:LongTimePattern"] = "h:mm:ss tt",
            ["Format:DateTime:ShortDatePattern"] = "M/d/yyyy",
            ["Format:DateTime:ShortTimePattern"] = "h:mm tt",
    
            // 金额格式化
            ["Format:CurrencyDecimal:Digits"] = "2",
            ["Format:CurrencyDecimal:Symbol"] = "$",
          };
          var source = new MemoryConfigurationSource
          {
            InitialData = initialData
          };
    
          IConfiguration configuration = new ConfigurationBuilder()
                  .Add(source)
                  .Build().GetSection("Format");
    
    
          IOptions<FormatSettings> optionsAccessor = new ServiceCollection()
              .AddOptions()
              .Configure<FormatSettings>(configuration)
              .BuildServiceProvider()
              .GetService<IOptions<FormatSettings>>();
    
          FormatSettings settings = optionsAccessor.Value;
    
          return settings;
        }
    
        /// <summary>
        /// 格式设置类
        /// </summary>
        public class FormatSettings
        {
          public DateTimeFormatSettings DateTime { get; set; }
          public CurrencyDecimalFormatSettings CurrencyDecimal { get; set; }
    
        }
    
        /// <summary>
        /// 时间格式化设置
        /// </summary>
        public class DateTimeFormatSettings
        {
          public string LongDatePattern { get; set; }
          public string LongTimePattern { get; set; }
          public string ShortDatePattern { get; set; }
          public string ShortTimePattern { get; set; }
        }
    
        /// <summary>
        /// 货币格式化设置
        /// </summary>
        public class CurrencyDecimalFormatSettings
        {
          public int Digits { get; set; }
          public string Symbol { get; set; }
        }
  • 相关阅读:
    Python中字符的练习
    Python中的数据结构 --- 集合(set)
    Python中的数据结构 --- 元组(tuple)、字典(tuple)
    Python中的数据结构 --- 列表(list)
    Python变量、字符练习1
    Python中字符串的操作
    Python中for、while、break、continue、if的使用
    Python中的变量
    Python中注释的添加
    Python的介绍及Pycharm软件的安装
  • 原文地址:https://www.cnblogs.com/WinHEC/p/9289841.html
Copyright © 2011-2022 走看看