Beispiel #1
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));
        }
        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);
            }
        }