Nucs / JsonSettings

This library simplifies creating configuration for your C# app/service by utilizing the serialization capabilities of Json.NET to serialize nested (custom) objects, dictionaries and lists as simply as by creating a POCO and inheriting JsonSettings class.
MIT License
76 stars 18 forks source link

Forced to reload information #15

Closed ghost1372 closed 3 years ago

ghost1372 commented 3 years ago

Hi Is there a way to force the settings to receive new information? for example In Window A, I set the following changes: I define JsonSettings.Load at the top of each class Untitled

Setting.IsFirstRun = true;

When I go to Window B, I get Old information instead of new information var newVal = Setting.IsFirstRun;

If I use the following code where I want to get new information, the problem will be solved

Settings SettingX = JsonSettings.Load<Settings>().EnableAutosave();
var newVal = SettingX.IsFirstRun;

Is there a better way to get the latest changes without redefining the settings?

Nucs commented 3 years ago

There is no way to do that built in, The design was to read json once, did not anticipate it to be changed and reloaded during runtime therefore there is no way to listen for changes - nor updating the object on the fly. That shouldn't stop you from creating a class that does just that. Listen to changes in that file using window's api (theres a managed way) and every time there is a change, reload it and store staticly (so the entire app reloads it once and only when its changed). So when you ask for settings, you get "snapshot" of how the file looked at that moment. Every time you need to do something with it, you can access the static provider and get the latest from them. or if you dont care about how old it is then just store the result in your object for later use.

ghost1372 commented 3 years ago

thank you I understood