/// <summary> /// Adds a new value with the key specified, copying the properties and elements from the value give, /// returning the new value. /// </summary> /// <param name="key"> /// The key identifying the value to add. /// </param> /// <param name="value"> /// The value to copy. /// </param> /// <returns> /// The newly added element. /// </returns> /// <remarks> /// Used to add objects and collections that have been constructed externally using alternate implementations. /// </remarks> public TElement AddCopy(TKey key, TElement value) { Debug.Assert(PropertyDef.ElementImplementation != null); var copy = PropertyDef.CopyValue(PropertyDef.ElementImplementation, value, this, ConfigurationRoot); Add(key, copy); return(copy); }
public void TestCopyValue() { var root = _propertyTestData.GetContext().Configuration.ConfigurationRoot; var propertyInfo = typeof(IRootElement).GetProperty(nameof(IRootElement.Int32PropertyB), BindingFlags.Instance | BindingFlags.Public); var propertyDef = new PropertyDef(propertyInfo, new ConfigurationObjectSettings()); var copy = propertyDef.CopyValue(propertyDef.Implementation, 123, null, root); Assert.Equal(123, copy); }
public void TestCopyValue_Object() { var root = _propertyTestData.GetContext().Configuration.ConfigurationRoot; var propertyInfo = typeof(IRootElement).GetProperty(nameof(IRootElement.ChildElementProperty), BindingFlags.Instance | BindingFlags.Public); var propertyDef = new PropertyDef(propertyInfo, new ConfigurationObjectSettings()); var child = new ChildElementMock() { Name = "NAME-1", Value = 1 }; var copy = propertyDef.CopyValue <IChildElement>(propertyDef.Implementation, child, null, root); Assert.NotNull(copy); Assert.Equal(child.Name, copy.Name); Assert.Equal(child.Value, copy.Value); copy = propertyDef.CopyValue <IChildElement>(propertyDef.Implementation, null, null, root); Assert.Null(copy); }
public void TestCopyValue_Dictionary() { var root = _propertyTestData.GetContext().Configuration.ConfigurationRoot; var propertyInfo = typeof(IRootElement).GetProperty(nameof(IRootElement.ChildDictionary), BindingFlags.Instance | BindingFlags.Public); var propertyDef = new PropertyDef(propertyInfo, new ConfigurationObjectSettings()); var dictionary = new ConfigurationDictionary <IChildElement>(null, propertyDef, root, new ConfigurationObjectSettings()); dictionary.AddCopy("A", new ChildElementMock() { Name = "NAME-1", Value = 1 }); dictionary.AddCopy("B", new ChildElementMock() { Name = "NAME-2", Value = 2 }); dictionary.AddCopy("C", new ChildElementMock() { Name = "NAME-3", Value = 3 }); var copy = propertyDef.CopyValue(propertyDef.Implementation, dictionary, null, root); Assert.NotNull(copy); Assert.Equal(dictionary.Count, copy.Count); foreach (var k in dictionary.Keys) { Assert.NotNull(copy[k]); Assert.Equal(dictionary[k].Value.Name, copy[k].Value.Name); Assert.Equal(dictionary[k].Value.Value, copy[k].Value.Value); } }
public void TestCopyValue_Collection() { var root = _propertyTestData.GetContext().Configuration.ConfigurationRoot; var propertyInfo = typeof(IRootElement).GetProperty(nameof(IRootElement.ChildCollection), BindingFlags.Instance | BindingFlags.Public); var propertyDef = new PropertyDef(propertyInfo, new ConfigurationObjectSettings()); var collection = new ConfigurationCollection <IChildElement>(null, propertyDef, root, new ConfigurationObjectSettings()); collection.AddCopy(new ChildElementMock() { Name = "NAME-1", Value = 1 }); collection.AddCopy(new ChildElementMock() { Name = "NAME-2", Value = 2 }); collection.AddCopy(new ChildElementMock() { Name = "NAME-3", Value = 3 }); var copy = propertyDef.CopyValue(propertyDef.Implementation, collection, null, root); Assert.NotNull(copy); Assert.Equal(collection.Count, copy.Count); for (var n = 0; n < collection.Count; ++n) { Assert.NotNull(copy[n]); Assert.Equal(collection[n].Name, copy[n].Name); Assert.Equal(collection[n].Value, copy[n].Value); } }