Building a JSON Configuration Section

后端 未结 5 1407
轮回少年
轮回少年 2021-02-18 20:37

Is there a way to have configuration sections written in JSON instead of XML?

Let\'s suppose I have the following ConfigurationSection:

publ         


        
5条回答
  •  日久生厌
    2021-02-18 21:23

    I'm recommending to use FX.configuration, you can add it from NuGet. you can find it at: http://nugetmusthaves.com/Package/FX.Configuration

    some code examples can be found at: https://bitbucket.org/friendlyx/fx.configuration

    it enables you to add to the App.config stuff like: < add key="JsonConfig" value="{ 'Id': '42', 'Name': 'foo' }" />

    another option when using the FX.configuration is to add a new config.json file with all your configuration and at the creation of the instance it will read and parse it.

    the following code does exactly what you need with the new json config file.

    using System.Collections.Generic;
    using FX.Configuration;
    namespace JsonConfigurationConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                var config = new Users();
            }
        }
        public class Users : JsonConfiguration
        {
            public List users { get; set; }
        }
        public class User
        {
            public string name { get; set; }
        }
    }
    

提交回复
热议问题