Sorry I couldn't think of a better title to describe the issue.
I use the following code to make it easier to update specific values of config properties. Note that the config properties are not just integers and there are not just 2 of them, just simplified it for this example.
public class Config {
public int VarA { get; set; }
public int VarB { get; set; }
}
private Config config;
private void Update(Config newValues) {
PropertyInfo[] properties = typeof(Config).GetProperties();
foreach (PropertyInfo property in properties) {
object n = property.GetValue(newValues);
property.SetValue(config, n ?? property.GetValue(config));
}
}
The Update method checks the properties of newValues and will update the properties of config if a value is defined.
I initialise config with values like so (just an example):
config = new Config() { VarA = 1, VarB = 2 };
Debug.WriteLine(config.VarA + " : " + config.VarB); // 1 : 2
Then if I only want to update VarA to a value of 0 and don't touch VarB, I do this:
Update(new Config() { VarA = 0 });
Debug.WriteLine(config.VarA + " : " + config.VarB); // 0 : 0
But this results in VarB also being set to 0 because newValues didn't have a value assigned for it and null value as int is 0 because int is non-nullable. How would I make VarB remain as value of 2 when it's not defined in newValues?