Created
February 28, 2020 16:37
-
-
Save pluveto/536c843b1fd85ce53e82a8ed2a94025f to your computer and use it in GitHub Desktop.
Json config manager / helper for c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 配置文件对象管理器 | |
/// </summary> | |
public class ConfigManager<T> | |
{ | |
/// <summary> | |
/// 缓存到内存的配置文件 | |
/// </summary> | |
public string FileName { get; private set; } | |
public T Object; | |
public ConfigManager(string fileName = "config.json") | |
{ | |
this.FileName = fileName; | |
} | |
public T Load() | |
{ | |
var json = File.ReadAllText(this.FileName); | |
this.Object = JsonConvert.DeserializeObject<T>(json); | |
return this.Object; | |
} | |
public void Save() | |
{ | |
var json = JsonConvert.SerializeObject(Object); | |
File.WriteAllText(this.FileName, json); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment