Exemple #1
0
        public void ConfigurationDataPreprocessor_correctly_stores_type_info()
        {
            // checks for TestConfigurationData type, defined in ConfigurationDataTest.cs
            Assert.IsTrue(_processor.Contains("Test"));
            ConfigurationTypeInfo cti = _processor.GetTypeInfo("Test");

            Assert.IsNotNull(cti);
            Assert.AreEqual(typeof(TestConfigurationData), cti.ObjectType);
        }
Exemple #2
0
        public void ConfigurationDataProcessor_correctly_creates_instance()
        {
            ConfigurationTypeInfo cti = _processor.GetTypeInfo("Test");

            Assert.IsNotNull(cti);
            object data = cti.CreateInstance <object>();

            Assert.IsNotNull(data);
            Assert.IsInstanceOfType(data, typeof(TestConfigurationData));
        }
        private void SetPropertyValue(XElement element, ConfigurationTypeInfo typeInfo, IConfigurationData <T> data, string propertyName, string propertyValue)
        {
            ConfigurationPropertyInfo propertyInfo = typeInfo.GetPropertyInfo(propertyName);

            if (propertyInfo == null)
            {
                throw new XmlConfigurationException(element, "Unknown property '{0}'", propertyName);
            }

            object value = ConvertFromString(element, propertyInfo, propertyValue);

            propertyInfo.Property.SetValue(data, value, null);
        }
        public void HandleElement(XElement ancestor)
        {
            if (ancestor == null)
            {
                throw new ArgumentNullException("ancestor");
            }
            if (!ElementName.Equals(ancestor.Name.LocalName, StringComparison.OrdinalIgnoreCase))
            {
                throw new XmlConfigurationException(ancestor, "Unexpected element '{0}' (expecting '{1}').",
                                                    ancestor.Name.LocalName, ElementName);
            }

            IList <T> result = new List <T>();

            foreach (XElement element in ancestor.Elements())
            {
                string typeName = element.Name.LocalName;
                if (!_collector.Value.Contains(typeName))
                {
                    throw new XmlConfigurationException(element, "Unknown element");
                }

                ConfigurationTypeInfo  typeInfo = _collector.Value.GetTypeInfo(typeName);
                IConfigurationData <T> data     = typeInfo.CreateInstance <IConfigurationData <T> >();

                var attributes  = element.Attributes().Select(x => new KeyValuePair <string, string>(x.Name.LocalName, x.Value));
                var subelements = element.Elements().Select(x => new KeyValuePair <string, string>(x.Name.LocalName, x.Value));

                foreach (var kv in attributes.Concat(subelements))
                {
                    SetPropertyValue(element, typeInfo, data, kv.Key, kv.Value);
                }

                T      item = data.CreateObject();
                string id   = GetItemId(item);
                if (_items.ContainsKey(id))
                {
                    throw new XmlConfigurationException(element, "Duplicate item (id '{0}') found.", id);
                }
                _items.Add(id, item);
            }
        }