public void BinarySerializeTest() { var person1 = new HelperObjects.SerializeAttribute.Person() { Salutation = "MR", FirstName = "Bob", LastName = "Smith", Age = 28 }; var person1Bytes = Serialization.BinarySerialize(person1); Expect(person1Bytes, Is.Not.Null); Expect(person1Bytes.Length, Is.GreaterThan(0)); var ms = new MemoryStream(person1Bytes); var bf = new BinaryFormatter(); var person2 = bf.Deserialize(ms) as Person; Expect(person2, Is.Not.Null); Expect(person2.Salutation, Is.EqualTo(person1.Salutation)); Expect(person2.FirstName, Is.EqualTo(person1.FirstName)); Expect(person2.LastName, Is.EqualTo(person1.LastName)); Expect(person2.Age, Is.EqualTo(person1.Age)); }
public void ListStringTest() { var stringsIn = new List <string> { "Item1", "Item2" }; var buffer = Serialization.BinarySerialize(stringsIn); var stringsOut = Serialization.BinaryDeserialize <List <string> >(buffer); stringsOut.Should().NotBeNull(); stringsOut.Count.Should().Be(2); stringsOut[0].Should().Be("Item1"); stringsOut[1].Should().Be("Item2"); }
/// <summary> /// Add multiple items to the cache /// </summary> /// <param name="items">The dictionary of objects to add to cache, with cache key as key and item as value.</param> public override void Add(Dictionary <string, object> items) { // convert to binary var binaryItems = new Dictionary <string, byte[]>(); foreach (var item in items) { binaryItems.Add( this.Prefix(item.Key), Serialization.BinarySerialize(item.Value)); } IndexusDistributionCache.SharedCache.MultiAdd(binaryItems); }
public void DictionaryStringStringTest() { var dictIn = new Dictionary <string, string> { { "Key1", "Value1" }, { "Key2", "Value2" } }; var buffer = Serialization.BinarySerialize(dictIn); var dictOut = Serialization.BinaryDeserialize <Dictionary <string, string> >(buffer); dictOut.Should().NotBeNull(); dictOut.Count.Should().Be(2); dictOut.Keys.First().Should().Be("Key1"); dictOut.Values.First().Should().Be("Value1"); dictOut.Keys.Last().Should().Be("Key2"); dictOut.Values.Last().Should().Be("Value2"); }
/// <summary> /// 添加多个对象 /// </summary> /// <param name="data"></param> public override void AddObjects <T>(IDictionary <string, T> data) { lock (lockObject) { IDictionary <string, byte[]> cacheData = new Dictionary <string, byte[]>(); foreach (KeyValuePair <string, T> kv in data) { if (kv.Value != null) { cacheData.Add(GetInputKey(kv.Key), Serialization.BinarySerialize(kv.Value)); } } dataCache.MultiAdd(cacheData); } }
/// <summary> /// Save /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuFileSave_Click(object sender, RoutedEventArgs e) { if (fileName == string.Empty) { // If not saved before, go to save as SaveAs(); } else { try { // Save (serialize) Serialization serialization = new Serialization(); serialization.BinarySerialize(fileName, taskMgr, projectMgr); MessageBox.Show("File saved", "Success"); } catch (Exception ex) { MessageBox.Show(string.Format("Error:\n{0}", ex.ToString()), "Error"); } } }
/// <summary> /// Save as /// </summary> private void SaveAs() { // Open dialog SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory(); saveFileDialog.Filter = "Data file (.dat)|*.bin|All Files *.*|*.*"; if (saveFileDialog.ShowDialog() == true) { fileName = saveFileDialog.FileName; try { // Save (serialize) Serialization serialization = new Serialization(); serialization.BinarySerialize(fileName, taskMgr, projectMgr); MessageBox.Show("File saved", "Success"); this.Title = string.Concat("Task Manager - ", System.IO.Path.GetFileName(fileName)); } catch (Exception ex) { MessageBox.Show(string.Format("Error:\n{0}", ex.ToString()), "Error"); } } }