.net core Configuration get array method

var cb=new ConfigurationBuilder();
cb.AddJsonFile(@".\appsettings.json");
var config=cb.Build();
var x2=config.GetSection("loads").Get<string[]>(); 
//var x3=config.GetChildren();
Console.WriteLine(string.Join(",",x2));

 

//其它的方式不太行,也可以使用

~.GetSection("pointKey").AsEnumerable();

但这个方法回来的值包括了很多其它东西,不是纯粹的值,把各种键都包括进来了,包括数组的键,还有方括号等等

所以推荐的方式就是:

~.GetSection("pointKey").Get<T[]>();

//

还有就是  ~.GetSection("pointKey").GetChildren()

这个方法也是对付集合的那方式比如下面代码

                foreach (var item in config.GetSection("GrpcChannels").GetChildren()) {
                    ChannelDic.Add(item["Key"], Grpc.Net.Client.GrpcChannel.ForAddress(item["Value"]));
                }

 

posted on 2020-04-09 21:51  ProjectDD  阅读(298)  评论(0编辑  收藏  举报