Closed laodc closed 5 years ago
Here is how the construction looks in JsonSettings
.
protected JsonSettings() {
Modulation = new ModuleSocket(this);
_childtype = GetType();
if (!_childtype.HasDefaultConstructor())
throw new JsonSettingsException($"Can't initiate a settings object with class that doesn't have empty public constructor.");
}
protected JsonSettings(string fileName) : this() { FileName = fileName; }
As you can see, simply constructing it does not invoke loading or saving but the constructors are protected.
Now consider the following static method ofJsonSettings
:
/// <summary>
/// Constucts a settings object for further configuration.
/// </summary>
/// <param name="args">The arguments that will be passed into the constructor.</param>
/// <returns>A freshly new object.</returns>
public static T Construct<T>(params object[] args) where T : ISavable {
JsonSettings o = (JsonSettings) (ISavable) Activator.CreateInstance(typeof(T), args);
o.EnsureConfigured();
return (T) (object) o;
}
Which creates a new instance of JsonSettings
without loading or saving.
Therefore, the following code will create a new instance without any interaction of existing ones.
MySettingsClass my_new_empty_settings = JsonSettings.Construct<MySettingsClass>();
Ahh, I need to call .Construct
I hadn't noticed that. Might be an idea to give an example showing this method.
Added an example to readme.md (b38afeff0dd3ca8f80232763e1d66c4a12989107), cheers.
When initializing a new
JsonSettings<MyClass>
I currently don't want it to create the file, i.e. pass it anull
I want to be able to access all my attributes and set/get them accordingly and then just
.Save( string )
when the time comes I actually want to save.The reason being, if a user currently just uses the default filename, when I want to create a new one, it simply loads the default and starts populating the data. assigning a random filename is simply a weird option. and i don't want to through in a bunch of IF checks as an alternative either.
Am I possibly missing a method that would achieve what I want?