using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace LB_SmartVisionCommon
|
{
|
/// <summary>
|
/// T类型-JSON--Serialize和Deserialize
|
/// </summary>
|
/// <typeparam name="T">T类型</typeparam>
|
public static class ConfigManager<T>
|
{
|
/// <summary>
|
/// 保存配置
|
/// </summary>
|
/// <typeparam name="T">T类型</typeparam>
|
/// <param name="config">T类型的值</param>
|
/// <param name="filePath">文件存储路径以及名称</param>
|
public static void SaveConfig<T>(T config, string filePath)
|
{
|
var json = JsonConvert.SerializeObject(config, Newtonsoft.Json.Formatting.Indented);
|
File.WriteAllText(filePath, json);
|
}
|
/// <summary>
|
/// 加载配置文件
|
/// </summary>
|
/// <typeparam name="T">T类型</typeparam>
|
/// <param name="filePath">文件存储路径以及名称</param>
|
/// <returns>T类型的值</returns>
|
public static T LoadConfig<T>(string filePath) where T : new()
|
{
|
if (!File.Exists(filePath))
|
{
|
return new T();
|
}
|
|
var json = File.ReadAllText(filePath);
|
return JsonConvert.DeserializeObject<T>(json);
|
}
|
}
|
}
|