示例#1
0
        public async Task ImportAsync(string file, ConfigurationManager manager)
        {
            List <ConfigurationContainer> containers = new List <ConfigurationContainer>();
            string json = "";

            using (StreamReader sr = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read)))
            {
                json = await sr.ReadToEndAsync();
            }

            if (string.IsNullOrWhiteSpace(json))
            {
                throw new ArgumentNullException("File contains no data");
            }

            object o = JsonConvert.DeserializeObject(json);

            if (o is JObject ob)
            {
                foreach (JToken container in ob.Children())
                {
                    if (container is JProperty property)
                    {
                        ConfigurationContainer newContainer = manager.Containers.FirstOrDefault(x => x.Type.Name == property.Name);

                        if (newContainer != null)
                        {
                            foreach (JObject value in property.Children())
                            {
                                foreach (JToken obj in value.Children())
                                {
                                    if (obj is JProperty valueProperty)
                                    {
                                        IConfigurationItem item = newContainer.ConfigurableItems.FirstOrDefault(x => x.Type.Name == valueProperty.Name);
                                        item.SetValue(valueProperty.Value.ToObject(item.Type.PropertyType));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        public void RegisterType(Type type, object instance)
        {
            var configurables = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                .Where(y => y.GetCustomAttributes <ConfigurableAttribute>().Count() > 0).ToList();

            if (configurables.Count == 0)
            {
                return;
            }

            ConfigurationContainer container = new ConfigurationContainer(type, instance);

            foreach (var configurable in configurables)
            {
                container.AddType(configurable);
            }

            configurationItems.Add(container);
        }
        public async Task ImportAsync(string file, ConfigurationManager manager)
        {
            List <ConfigurationContainer> containers = new List <ConfigurationContainer>();

            StreamReader sr          = new StreamReader(file);
            string       fileContent = await sr.ReadToEndAsync();

            object o = JsonConvert.DeserializeObject(fileContent);

            if (o is JObject ob)
            {
                foreach (JToken container in ob.Children())
                {
                    if (container is JProperty property)
                    {
                        ConfigurationContainer newContainer = manager.Containers.FirstOrDefault(x => x.Type.Name == property.Name);
                        if (newContainer != null)
                        {
                            foreach (JObject value in property.Children())
                            {
                                foreach (JToken obj in value.Children())
                                {
                                    if (obj is JProperty valueProperty)
                                    {
                                        IConfigurationItem item = newContainer.ConfigurableItems.FirstOrDefault(x => x.Type.Name == valueProperty.Name);
                                        item.SetValue(valueProperty.Value.ToObject(item.Type.PropertyType));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            sr.Close();
        }