参考页面:

http://www.yuanjiaocheng.net/webapi/test-webapi.html

http://www.yuanjiaocheng.net/webapi/web-api-controller.html

http://www.yuanjiaocheng.net/webapi/config-webapi.html

http://www.yuanjiaocheng.net/webapi/web-api-route.html

http://www.yuanjiaocheng.net/webapi/parameter-binding.html

用于永久化对象,什么程序都行,依赖NewtonSoft。用于json序列化和反序列化。
 using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace ConfigHandler
 {
     public class ConfigHandler<T>
         where T : class
     {
         const string SAVE_PATH = "jsonconfig/";
         /// <summary>
         /// 单例模式
         /// </summary>
         static T config;
         private ConfigHandler()
         {

         }
         /// <summary>
         /// 获取保存地址,默认是泛型参数T的类型名称
         /// </summary>
         /// <returns></returns>
         private static string GetSavePath()
         {
             if (!Directory.Exists(SAVE_PATH))
             {
                 Directory.CreateDirectory(SAVE_PATH);
             }
             return $"{SAVE_PATH}{typeof(T).ToString()}.json";
         }
         /// <summary>
         /// 保存配置
         /// </summary>
         public static void Save(T _config)
         {
             config = _config;
             string json = JsonConvert.SerializeObject(_config);
             try
             {
                 using (var sw = new StreamWriter(GetSavePath()))
                 {
                     sw.WriteAsync(json);
                 }

             }
             catch (Exception)
             {
                 throw;
             }
         }
         /// <summary>
         /// 获取配置信息
         /// </summary>
         /// <returns></returns>
         public static T Load()
         {
             if (config == null)
             {
                 string json = "";
                 try
                 {
                     using (var sr = new StreamReader(GetSavePath()))
                     {
                         json = sr.ReadToEnd();
                         if (json != "")
                         {
                             config = JsonConvert.DeserializeObject<T>(json);
                         }
                         else
                         {
                             config = null;
                         }
                     }
                 }
                 catch (Exception)
                 {
                     config = null;
                 }
             }
             return config;
         }

     }

 }

demo:

using ConfigHandler;
using ConsoleApplication1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyConfig config = new MyConfig();
            config = new MyConfig();
            config.name = "leiming";
            config.Age = ;
            config.Time = DateTime.Now;
            ConfigHandler<MyConfig>.Save(config);
            config = ConfigHandler<MyConfig>.Load();
            Console.WriteLine(config.ToString());
            Console.ReadKey();
        }
    }
    class MyConfig
    {
        public int Hello{get;set;}
        public string name { get; set; }
        public int Age { get; set; }
        public DateTime Time { get; set; }
        public override string ToString()
        {
            return $"Name={name},Age={Age},Time={Time.ToShortDateString()}";
        }
    }
}

C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置的更多相关文章

  1. C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置

    用于永久化对象,什么程序都行,依赖NewtonSoft.用于json序列化和反序列化. using Newtonsoft.Json; using System; using System.Collec ...

  2. .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

    JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...

  3. python对象转化为json串、json串转化为python串

    1.1. JSON简介: JavaScript Object Notation(JavaScript对象表示法) JSON是存储和交换文本信息的语法.类似XML JSON比XML更小.更快.更易解析 ...

  4. JVM总括四-类加载过程、双亲委派模型、对象实例化过程

    JVM总括四-类加载过程.双亲委派模型.对象实例化过程 目录:JVM总括:目录 一. 类加载过程 类加载过程就是将.class文件转化为Class对象,类实例化的过程,(User user = new ...

  5. Java技巧——将前端的对象数组通过Json字符串传到后端并转换为对象集合

    Java技巧——将前端的对象数组通过Json字符串传到后端并转换为对象集合 摘要:本文主要记录了如何将将前端的对象数组通过Json字符串传到后端,并在后端将Json字符串转换为对象集合. 前端代码 前 ...

  6. .NET(C#)中的DataSet、string、DataTable等对象转换成Json

    ConvertJson.cs类 using System; using System.Collections.Generic; using System.Text; using System.Data ...

  7. Newtonsoft.Json 把对象转换成json字符串

    var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...

  8. JSON转换类(二)--List转换成Json、对象集合转换Json等

    #region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...

  9. json 字符串转换成对象,对象转换成json字符串

    json   字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法:   [注意jquery版本问题] var str = '{"name":&qu ...

随机推荐

  1. 加深一下BlockingQueue的认识

    认识BlockingQueue BlockingQueue是一种可以阻塞线程的队列,java中对这种队列提供了方法抽象,BlockingQueue则是抽象的接口. add:添加元素到队列里,添加成功返 ...

  2. Java 输出流中的flush方法

    转自:http://blog.csdn.net/jiyangsb/article/details/50984440 java中的IO流中的输出流一般都有flush这个操作,这个操作的作用是强制将缓存中 ...

  3. 旺财速啃H5框架之Bootstrap(一)

    接下来的时间里,我将和大家一起对当前非常流行的前端框架Bootstrap进行速度的学习,以案例的形式.对刚开始想学习Bootstrap的同学而找不着边的就很有帮助了.如果你想详细的学习Bootstra ...

  4. iOS审核这些坑,腾讯游戏也踩过

    作者:Jamie,专项技术测试工程师,在iOS预审和ASO优化领域从事专项测试相关工作,为腾讯游戏近100个产品提供专项服务. WeTest 导读 在App上架苹果应用商店的过程中,相信大多数iOS开 ...

  5. BPM嵌入式流程解决方案分享

    一.需求分析由于企业业务的独特性或者企业高层独特的管理思想,很多客户选择了自行开发业务系统的方式来实现独有的竞争力. 这类信息系统通常经过了多年的开发,伴随着企业的发展一直在不断优化,与企业的业务非常 ...

  6. H3 BPM:为石化企业提供一个不一样的全停大修平台

    H3 BPM大型炼化企业装置全停检修管理平台(简称"全停大修")结合国际化的流程管理理念.成熟的系统技术架构.优秀的行业解决方案,为石油化工行业全停大修提供了卓越的信息化管理方案, ...

  7. web.xml中welcome-file-list的作用

    今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...

  8. xamarin绑定原生库的一些坑

    最近一个项目涉及到较多的第三方库的绑定技术,中间遇到了几个坑,记录下来与大家分享 绑定Jar库 monoandroid对原生库的调用都通过Android.Runtime.JNIEnv进行调入(http ...

  9. DotNet Run 命令介绍

    前言 本篇主要介绍 asp.net core 中,使用 dotnet tools 运行 dotnet run 之后的系统执行过程. 如果你觉得对你有帮助的话,不妨点个[推荐]. 目录 dotnet r ...

  10. 自制文件上传JS控件,支持IE、chrome、firefox etc.

    (function() { if (window.FileUpload) { return; } window.FileUpload = function (id, url) { this.id = ...