public override void Validate(object value)
        {
            ConfigurationElementCollection collection = (ConfigurationElementCollection)value;

            if (collection.Count < _minimumCount)
            {
                throw new ConfigurationErrorsException(string.Format(
                                                           "The collection of type '{0}' must contain at least {1} elements, but contained {2}.",
                                                           collection.GetType().Name,
                                                           _minimumCount.ToString(),
                                                           collection.Count.ToString()));
            }
        }
Example #2
0
        /// <summary>
        /// Add configuration element to a collection and set unique name.
        /// Warning do not scan for child element in the collection
        /// </summary>
        /// <param name="globalCollection">collection</param>
        /// <param name="e">element to add</param>
        /// <param name="prefix">prefix to use in name</param>
        /// <param name="prefix">Collection to add new item if null item is added on globalCollection</param>
        public static string AddWithUniqueName(ConfigurationElementCollection globalCollection, ConfigurationElement e, string prefix, ConfigurationElementCollection parentCollection = null, int position = -1)
        {
            var i            = 0;
            var propertyInfo = e.GetType().GetProperty("Nom");

            while (true)
            {
                i++;
                var name = prefix + i;

                var type = globalCollection.GetType();

                if ((bool)type.GetMethod("Contains", new[] { typeof(string) }).Invoke(globalCollection, new object[] { name }))
                {
                    continue;
                }

                propertyInfo.SetValue(e, name, null);

                var method  = position == -1 ? "Add" : "Insert";
                var types   = position == -1 ? new[] { typeof(ConfigurationElement) } : new[] { typeof(int), typeof(ConfigurationElement) };
                var objects = position == -1 ? new object[] { e } : new object[] { 0, e };


                if (parentCollection == null)
                {
                    type.GetMethod(method, types).Invoke(globalCollection, objects);
                }
                else
                {
                    var parentType = parentCollection.GetType();
                    parentType.GetMethod(method, types).Invoke(parentCollection, objects);
                }

                return(name);
            }
        }
Example #3
0
        ///<summary>
        /// Creates a <see cref="IMergeableConfigurationElementCollection"/> based on a ConfigurationElementCollection type.
        ///</summary>
        ///<param name="collection"></param>
        ///<returns></returns>
        public static IMergeableConfigurationElementCollection GetCreateMergeableCollection(ConfigurationElementCollection collection)
        {
            if (collection is IMergeableConfigurationElementCollection)
            {
                return((IMergeableConfigurationElementCollection)collection);
            }
            else if (collection is ConnectionStringSettingsCollection)
            {
                return(new MergeableConnectionStringSettingsCollection((ConnectionStringSettingsCollection)collection));
            }
            else if (collection is KeyValueConfigurationCollection)
            {
                return(new MergeableKeyValueConfigurationCollection((KeyValueConfigurationCollection)collection));
            }

            var mergeableConfigurationCollectionAttribute = collection.GetType().GetTypeInfo().GetCustomAttributes <MergeableConfigurationCollectionTypeAttribute>().FirstOrDefault();

            if (mergeableConfigurationCollectionAttribute != null)
            {
                return(Activator.CreateInstance(mergeableConfigurationCollectionAttribute.MergeableConfigurationCollectionType, collection) as IMergeableConfigurationElementCollection);
            }

            return(null);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sourceElement"></param>
        /// <param name="targetElement"></param>
        /// <returns></returns>
        public static ConfigurationElement CloneElement(ConfigurationElement sourceElement, ConfigurationElement targetElement)
        {
            if (sourceElement is ICustomProviderData)
            {
                var targetAttributes = ((ICustomProviderData)targetElement).Attributes;
                var sourceAttributes = ((ICustomProviderData)sourceElement).Attributes;
                foreach (string key in sourceAttributes)
                {
                    targetAttributes.Add(key, sourceAttributes[key]);
                }
            }

            foreach (PropertyInformation property in sourceElement.ElementInformation.Properties)
            {
                if (property.ValueOrigin == PropertyValueOrigin.Default)
                {
                    continue;
                }
                try
                {
                    if (property.Value == null)
                    {
                        continue;
                    }
                }
                catch
                {
                    continue;
                }
                PropertyInformation targetProperty = targetElement.ElementInformation.Properties[property.Name];

                if (typeof(ConfigurationElement).IsAssignableFrom(property.Type) && typeof(ICloneableConfigurationElement).IsAssignableFrom(property.Type))
                {
                    targetProperty.Value = ((ICloneableConfigurationElement)property.Value).CreateFullClone();
                }
                if (typeof(ConfigurationElement).IsAssignableFrom(property.Type) && TypeDescriptor.GetAttributes(property.Type).OfType <CloneableConfigurationElementTypeAttribute>().Any())
                {
                    CloneableConfigurationElementTypeAttribute cloneableConfigurationElementTypeAttribute = TypeDescriptor.GetAttributes(property.Type).OfType <CloneableConfigurationElementTypeAttribute>().First();
                    ICloneableConfigurationElement             cloneable = (ICloneableConfigurationElement)Activator.CreateInstance(cloneableConfigurationElementTypeAttribute.CloneableConfigurationElementType, property.Value);

                    targetProperty.Value = cloneable.CreateFullClone();
                }
                if (typeof(ConfigurationElementCollection).IsAssignableFrom(property.Type))
                {
                    ConfigurationElementCollection sourceCollection = (ConfigurationElementCollection)property.Value;
                    ConfigurationElementCollection targetCollection = (ConfigurationElementCollection)Activator.CreateInstance(sourceCollection.GetType());
                    targetCollection = CloneCollection(sourceCollection, targetCollection);

                    targetProperty.Value = targetCollection;
                }
                else if (typeof(ConfigurationElement).IsAssignableFrom(property.Type))
                {
                    ConfigurationElement sourceChildElement = (ConfigurationElement)property.Value;
                    ConfigurationElement targetChildElement = (ConfigurationElement)Activator.CreateInstance(sourceChildElement.GetType());

                    targetChildElement   = CloneElement(sourceChildElement, targetChildElement);
                    targetProperty.Value = targetChildElement;
                }
                else
                {
                    targetProperty.Value = property.Value;
                }
            }

            return(targetElement);
        }
Example #5
0
        public void ReadConfigSectionsFromFile()
        {
            using (var temp = new TempConfig(DiagnosticsTestData.Sample))
            {
                var config = ConfigurationManager.OpenExeConfiguration(temp.ExePath);

                ConfigurationSection section = config.GetSection("system.diagnostics");
                Assert.NotNull(section);
                Assert.Equal("SystemDiagnosticsSection", section.GetType().Name);

                ConfigurationElementCollection collection;
                ConfigurationElement[]         items;

                // Verify Switches
                collection = (ConfigurationElementCollection)GetPropertyValue("Switches");
                Assert.Equal("SwitchElementsCollection", collection.GetType().Name);
                Assert.Equal(1, collection.Count);
                items = new ConfigurationElement[1];
                collection.CopyTo(items, 0);
                Assert.Equal("sourceSwitch", items[0].ElementInformation.Properties["name"].Value.ToString());
                Assert.Equal("Error", items[0].ElementInformation.Properties["value"].Value.ToString());

                // Verify SharedListeners
                collection = (ConfigurationElementCollection)GetPropertyValue("SharedListeners");
                Assert.Equal("SharedListenerElementsCollection", collection.GetType().Name);
                Assert.Equal(1, collection.Count);
                items = new ConfigurationElement[1];
                collection.CopyTo(items, 0);
                Assert.Equal("myListener", items[0].ElementInformation.Properties["name"].Value.ToString());
                Assert.Equal("System.Diagnostics.TextWriterTraceListener", items[0].ElementInformation.Properties["type"].Value.ToString());
                Assert.Equal("myListener.log", items[0].ElementInformation.Properties["initializeData"].Value.ToString());

                // Verify Sources
                collection = (ConfigurationElementCollection)GetPropertyValue("Sources");
                Assert.Equal("SourceElementsCollection", GetPropertyValue("Sources").GetType().Name);
                Assert.Equal(1, collection.Count);
                items = new ConfigurationElement[1];
                collection.CopyTo(items, 0);
                Assert.Equal("TraceSourceApp", items[0].ElementInformation.Properties["name"].Value.ToString());
                Assert.Equal("sourceSwitch", items[0].ElementInformation.Properties["switchName"].Value.ToString());
                Assert.Equal("System.Diagnostics.SourceSwitch", items[0].ElementInformation.Properties["switchType"].Value);

                ConfigurationElementCollection listeners = (ConfigurationElementCollection)items[0].ElementInformation.Properties["listeners"].Value;
                Assert.Equal("ListenerElementsCollection", listeners.GetType().Name);
                Assert.Equal(2, listeners.Count);
                ConfigurationElement[] listenerItems = new ConfigurationElement[2];
                listeners.CopyTo(listenerItems, 0);
                Assert.Equal("console", listenerItems[0].ElementInformation.Properties["name"].Value.ToString());
                Assert.Equal("System.Diagnostics.ConsoleTraceListener", listenerItems[0].ElementInformation.Properties["type"].Value.ToString());
                Assert.Equal("myListener", listenerItems[1].ElementInformation.Properties["name"].Value.ToString());

                ConfigurationElement filter = (ConfigurationElement)listenerItems[0].ElementInformation.Properties["filter"].Value;
                Assert.Equal("FilterElement", filter.GetType().Name);
                Assert.Equal("System.Diagnostics.EventTypeFilter", filter.ElementInformation.Properties["type"].Value.ToString());
                Assert.Equal("Error", filter.ElementInformation.Properties["initializeData"].Value.ToString());

                object GetPropertyValue(string propertyName) => section.GetType().
                GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance).
                GetValue(section);
            }
        }