Exemplo n.º 1
0
        private void ImportSettingsExecuted()
        {
            var dialog = new OpenFileDialog();

            dialog.Title  = "Import settings";
            dialog.Filter = "Config files (*.json)|*.json";
            dialog.ShowDialog();

            if (dialog.FileName != string.Empty)
            {
                var ser           = new DataContractJsonSerializer(typeof(Configuration));
                var stream        = new FileStream(dialog.FileName, FileMode.Open);
                var configuration = ser.ReadObject(stream) as Configuration;
                stream.Close();
                var properties = typeof(Configuration).GetProperties();
                foreach (var property in properties)
                {
                    if (property.PropertyType == typeof(IList <ConfigurationItem>))
                    {
                        var dataItems =
                            property.GetValue(configuration) as IList <ConfigurationItem>;
                        DataLayout.Clear();
                        if (dataItems != null)
                        {
                            foreach (var plusDataItem in dataItems)
                            {
                                DataLayout.Add(plusDataItem);
                            }
                        }
                    }
                    else if (property.PropertyType == typeof(IList <DirectHopItem>))
                    {
                        var directHops =
                            property.GetValue(configuration) as IList <DirectHopItem>;
                        DirectHops.Clear();
                        if (directHops != null)
                        {
                            foreach (var directHopItem in directHops)
                            {
                                DirectHops.Add(directHopItem);
                            }
                        }
                    }
                    else
                    {
                        var propertyInfo = GetType().GetProperty(property.Name);
                        if (propertyInfo != null)
                        {
                            propertyInfo.SetValue(this, property.GetValue(configuration));
                        }
                        else
                        {
                            throw new Exception("Property not found.");
                        }
                    }
                }
            }
        }