public void Get_and_set_should_work_regardless_of_culture() { /* Arrange */ var nv = new NameValueSettings(); var expectedDate = DateTime.UtcNow; var expectedTime = new TimeSpan(5, 4, 3, 2, 1); var expectedGuid = Guid.NewGuid(); /* Act */ using (new CultureContext()) { nv.SetSetting("StringValue", "SomeString"); nv.SetEncryptedSetting("StringValueTwo", "SomeOtherString"); nv.SetSetting(TestSetting.IntegerValue, 10); nv.SetSetting(TestSetting.BooleanValue, true); nv.SetSetting(TestSetting.DateTimeValue, expectedDate); nv.SetSetting(TestSetting.EnumValue, TestSetting.EnumValue); nv.SetSetting(TestSetting.TimeSpanValue, expectedTime); nv.SetEncryptedSetting(TestSetting.GuidValue, expectedGuid); } bool encryptedDate = nv.IsEncrypted(TestSetting.DateTimeValue); bool encryptedGuid = nv.IsEncrypted(TestSetting.GuidValue); bool valueExists = nv.HasSetting(TestSetting.BooleanValue); bool valueNoExists = nv.HasSetting("SomeOtherVal"); string stringVal = nv.Get("StringValue", null); string stringVal2 = nv.Get<string>("StringValueTwo", null); int intVal = nv.Get<int>(TestSetting.IntegerValue, 0); bool boolVal = nv.Get<bool>(TestSetting.BooleanValue, false); DateTime dateVal = nv.Get<DateTime>(TestSetting.DateTimeValue, DateTime.MaxValue); TestSetting enumVal = nv.Get<TestSetting>(TestSetting.EnumValue, TestSetting.BooleanValue); TimeSpan timeVal = nv.Get<TimeSpan>(TestSetting.TimeSpanValue, TimeSpan.Zero); Guid guidVal = nv.Get<Guid>(TestSetting.GuidValue, Guid.Empty); /* Assert */ stringVal.Should().Be("SomeString"); stringVal2.Should().Be("SomeOtherString"); intVal.Should().Be(10); boolVal.Should().Be(true); dateVal.Should().Be(expectedDate); enumVal.Should().Be(TestSetting.EnumValue); timeVal.Should().Be(expectedTime); guidVal.Should().Be(expectedGuid); encryptedDate.Should().BeFalse(); encryptedGuid.Should().BeTrue(); valueExists.Should().BeTrue(); valueNoExists.Should().BeFalse(); }
private static XElement WriteSettings(string sectionXmlName, NameValueSettings sectionSource, ICryptoProvider cryptoProvider, bool encryptAll) { //Write the settings var section = new XElement(sectionXmlName); var rawSource = sectionSource.RawSettings; foreach (string key in rawSource.AllKeys) { //Add the description if appropriate string description = sectionSource.GetDescription(key); if (description.Length > 0) { section.Add(new XComment(description)); } //Encrypt if appropriate string val = rawSource[key]; if (cryptoProvider != null && (encryptAll || sectionSource.IsEncrypted(key))) { val = cryptoProvider.Encrypt(val); } section.Add(new XElement( ConfigElement.KeyValueSettingNode, new XAttribute(ConfigElement.SettingKeyAttribute, key), new XAttribute(ConfigElement.SettingValueAttribute, val))); } return section; }