Example #1
0
 private void ButtonDownloadDeserializedObjects_Click(object sender, EventArgs e)
 {
     if (OpenFileDialog.ShowDialog() == DialogResult.OK)
     {
         string fileName          = OpenFileDialog.FileName;
         string serializedObjects = File.ReadAllText(OpenFileDialog.FileName);
         for (int i = 0; i < ComboBoxCoders.Items.Count; i++)
         {
             ICodingPlugin plugin = (ICodingPlugin)ComboBoxCoders.Items[i];
             if (plugin.Extension == fileName.Substring(fileName.LastIndexOf(".")))
             {
                 serializedObjects = Encoding.UTF8.GetString(plugin.Decode(serializedObjects));
                 fileName          = fileName.Substring(0, fileName.LastIndexOf("."));
             }
         }
         for (int i = 0; i < ComboBoxSerializers.Items.Count; i++)
         {
             SerializerFactory factory = (SerializerFactory)ComboBoxSerializers.Items[i];
             if (factory.Extension == fileName.Substring(fileName.LastIndexOf(".")))
             {
                 ISerializer serializer = factory.CreateSerializer();
                 applicationDataContext.Objects = serializer.Deserialize(serializedObjects);
                 applicationDataContext.ComboBoxObjectsRefresh();
                 return;
             }
         }
         MessageBox.Show("Сериализатора или плагина с таким расширением не найдено(");
     }
 }
Example #2
0
        public void Update(ApplicationDataContext applicationDataContext, bool NeedToCreate)
        {
            Form form = Controls.AddForm(this, ControlWidth, ControlHeight);
            int  y    = 0;

            GetProperties(form, this, ref y, applicationDataContext);
            Button saveButton = Controls.AddButton(form, y, 0, ControlWidth, ControlHeight, "Сохранить");

            saveButton.Click += (sender, e) =>
            {
                int i = 1;
                foreach (PropertyInfo property in this.GetType().GetProperties())
                {
                    if (property.PropertyType.IsEnum)
                    {
                        property.SetValue(this, Enum.Parse(property.PropertyType, form.Controls[i].Text));
                    }
                    else
                    if (property.PropertyType == typeof(int) || property.PropertyType == typeof(string))
                    {
                        property.SetValue(this, Convert.ChangeType(form.Controls[i].Text, property.PropertyType));
                    }
                    else
                    if (!property.PropertyType.IsGenericType && property.PropertyType.GetCustomAttributes(true).OfType <CommunicationTypeAttribute>().First().CommunicationType == "Агрегация")
                    {
                        ComboBox comboBox = (ComboBox)form.Controls[i];
                        if (comboBox.SelectedItem != null)
                        {
                            if (property.GetValue(this) != null)
                            {
                                foreach (var el in property.PropertyType.GetProperties())
                                {
                                    if (el.PropertyType.IsGenericType)
                                    {
                                        IList list = (IList)el.GetValue(property.GetValue(this));
                                        list.Remove(this);
                                    }
                                }
                            }
                            property.SetValue(this, comboBox.SelectedItem);
                            foreach (var el in property.PropertyType.GetProperties())
                            {
                                if (el.PropertyType.IsGenericType)
                                {
                                    IList list = (IList)el.GetValue(comboBox.SelectedItem);
                                    list.Add(this);
                                }
                            }
                        }
                    }
                    i += 2;
                }
                if (NeedToCreate)
                {
                    applicationDataContext.CallObjectCreatedEvent(applicationDataContext.Objects, this);
                }
                applicationDataContext.ComboBoxObjectsRefresh();
                form.Close();
            };
            Button cancelButton = Controls.AddButton(form, y, ControlWidth, ControlWidth, ControlHeight, "Отмена");

            cancelButton.Click += (sender, e) =>
            {
                form.Close();
            };
            form.Show();
        }