public static Armor Clone(Armor clonesource) { string jsonwrite = JsonConvert.SerializeObject(clonesource); Armor clone = JsonConvert.DeserializeObject<Armor>(jsonwrite); return clone; }
private void Armor_Save_Click(object sender, RoutedEventArgs e) { string sName = Armor_Name.Text; if (sName == "") { MessageBox.Show("You must enter a name for the weapon"); return; } int iAC = 0; if (!Int32.TryParse(Armor_AC.Text, out iAC)) { MessageBox.Show("You must enter a number for Armor Class"); return; } int iMR = 0; if (!Int32.TryParse(Armor_MovementRestriction.Text, out iMR)) { MessageBox.Show("You must enter a number for MovementRestriction"); return; } IEnumerable<Armor> ArmorQuery = Armors.Where(x => x.sName == sName); Armor writeArmor = null; if (ArmorQuery.Count() > 0) { writeArmor = ArmorQuery.First(); writeArmor.iAC = iAC; writeArmor.sName = sName; writeArmor.iMovementRestriction = iMR; } else { writeArmor = new Armor(iAC,iMR,sName); } if (Armor_ArmorsList.SelectedIndex != -1) { MessageBoxResult result = MessageBox.Show("You have selected an item do you want to override(yes),create new (no),or cancel", "Attention", MessageBoxButton.YesNoCancel); if (result == MessageBoxResult.Yes) { } if (result == MessageBoxResult.No) { Armor_ArmorsList.SelectedIndex = -1; } if (result == MessageBoxResult.Cancel) { return; } } if (ArmorQuery.Count() == 0) { Armors.Add(writeArmor); } Armor_ArmorsList.Items.Refresh(); string jsonwrite = JsonConvert.SerializeObject(writeArmor); WriteJsonStringToFile(jsonwrite, "armor", sName); foreach (Character character in Characters) { int armoroccurance = character.Armors.Where(x => x.sName == writeArmor.sName).Count(); if (armoroccurance > 0) { character.Armors.RemoveAll(x => x.sName == writeArmor.sName); for (int i = 0; i < armoroccurance; i++) { character.Armors.Add(writeArmor); UpdateUIElementsItems(); } } jsonwrite = JsonConvert.SerializeObject(character); WriteJsonStringToFile(jsonwrite, "character", character.Name); } }