/// <summary> /// 批量查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="keys"></param> /// <returns></returns> public IDictionary <string, T> GetAll <T>(IList <string> keys) where T : class, new() { if (keys == null || keys.Count == 0) { return(null); } IDictionary <string, T> dic = new Dictionary <string, T>(); IDictionary <string, string> data = redis.GetAll <string>(keys); if (data == null) { return(null); } foreach (var item in data) { if (item.Value != null) { dic.Add(item.Key, StringFormatter.Deserialize <T>(item.Value)); } else { dic.Add(item.Key, default(T)); } } return(dic); }
public void TestCycleSerializationDeserializationB() { ExampleA a_own = new ExampleA(1.0f, "testA", new DateTime(1998, 12, 1, 0, 0, 0), null); ExampleB b_own = new ExampleB(2.0f, "testB", new DateTime(1999, 11, 2, 0, 0, 0), null); ExampleC c_own = new ExampleC(3.0f, "testC", new DateTime(2000, 10, 3, 0, 0, 0), null); a_own.Reference = b_own; b_own.Reference = c_own; c_own.Reference = a_own; MemoryStream ms = new MemoryStream(); IFormatter formatter = new StringFormatter(new StringBinder()); formatter.Serialize(ms, b_own); ExampleB deserializedB = (ExampleB)formatter.Deserialize(new MemoryStream(ms.ToArray())); Assert.AreEqual(b_own.FloatTest, deserializedB.FloatTest); Assert.AreEqual(b_own.StringTest, deserializedB.StringTest); Assert.AreEqual(b_own.DateTest, deserializedB.DateTest); Assert.AreEqual(b_own.Reference.FloatTest, deserializedB.Reference.FloatTest); Assert.AreEqual(b_own.Reference.StringTest, deserializedB.Reference.StringTest); Assert.AreEqual(b_own.Reference.DateTest, deserializedB.Reference.DateTest); Assert.AreEqual(b_own.Reference.Reference.FloatTest, deserializedB.Reference.Reference.FloatTest); Assert.AreEqual(b_own.Reference.Reference.StringTest, deserializedB.Reference.Reference.StringTest); Assert.AreEqual(b_own.Reference.Reference.DateTest, deserializedB.Reference.Reference.DateTest); Assert.AreSame(deserializedB, deserializedB.Reference.Reference.Reference); Assert.AreSame(deserializedB.Reference, deserializedB.Reference.Reference.Reference.Reference); Assert.AreSame(deserializedB.Reference.Reference, deserializedB.Reference.Reference.Reference.Reference.Reference); }
/// <summary> /// 获取与键关联的值 /// </summary> /// <typeparam name="T">值类型</typeparam> /// <param name="key">键</param> /// <returns></returns> public T Get <T>(string key) where T : class, new() { string message = this.Get(key); if (string.IsNullOrEmpty(message)) { return(default(T)); } return(StringFormatter.Deserialize <T>(message)); }
public void TestNullReferenceSerializationDeserializationB() { ExampleB b = new ExampleB(1.0f, "testB", new DateTime(1998, 12, 1, 0, 0, 0), null); MemoryStream ms = new MemoryStream(); IFormatter formatter = new StringFormatter(new StringBinder()); formatter.Serialize(ms, b); ExampleB deserializedB = (ExampleB)formatter.Deserialize(new MemoryStream(ms.ToArray())); Assert.AreEqual(b.FloatTest, deserializedB.FloatTest); Assert.AreEqual(b.StringTest, deserializedB.StringTest); Assert.AreEqual(b.DateTest, deserializedB.DateTest); }
public void TestNullReferenceSerializationDeserializationA() { ExampleA a = new ExampleA(1.0f, "testA", new DateTime(1998, 12, 1, 0, 0, 0), null); MemoryStream ms = new MemoryStream(); IFormatter formatter = new StringFormatter(new StringBinder()); formatter.Serialize(ms, a); ExampleA deserializedA = (ExampleA)formatter.Deserialize(new MemoryStream(ms.ToArray())); Assert.AreEqual(a.FloatTest, deserializedA.FloatTest); Assert.AreEqual(a.StringTest, deserializedA.StringTest); Assert.AreEqual(a.DateTest, deserializedA.DateTest); }
public void TestNullReferenceSerializationDeserializationC() { ExampleC c = new ExampleC(1.0f, "testC", new DateTime(1998, 12, 1, 0, 0, 0), null); MemoryStream ms = new MemoryStream(); IFormatter formatter = new StringFormatter(new StringBinder()); formatter.Serialize(ms, c); ExampleC deserializedC = (ExampleC)formatter.Deserialize(new MemoryStream(ms.ToArray())); Assert.AreEqual(c.FloatTest, deserializedC.FloatTest); Assert.AreEqual(c.StringTest, deserializedC.StringTest); Assert.AreEqual(c.DateTest, deserializedC.DateTest); }
/// <summary> /// 订阅消息队列 /// </summary> /// <param name="chanels">消息队列</param> public void Subscribe <T>(string chanels, Action <T> handler) where T : class, new() { if (handler == null) { throw new ArgumentNullException("handler"); } if (string.IsNullOrEmpty(chanels)) { throw new ArgumentException("chanels"); } lock (m_contexts) { if (m_contexts.FirstOrDefault((i) => i.MqChanels == chanels) != null) { throw new ArgumentException("repeat"); } MqSubscribeContext context = new MqSubscribeContext { EventHandler = (message) => { T value = null; if (typeof(string) != typeof(T)) { if (!string.IsNullOrEmpty(message)) { value = StringFormatter.Deserialize <T>(message); } } else { object obj = message; value = (T)obj; } handler(value); }, EventType = typeof(T), MqChanels = chanels }; m_contexts.Add(context); } }
private void DoSerializationMenu(Format format) { if (format == Format.OWN) { bool run = true; bool walk = true; SerializationBinder binder = new StringBinder(); Formatter formatter = new StringFormatter(binder); while (run) { Console.WriteLine("\nDo you want to serialize [s] or deserialize [d]: \n"); char choice = Console.ReadKey().KeyChar; switch (choice) { case 's': ExampleA exampleA = new ExampleA(1.0f, "testA", new DateTime(1998, 12, 1, 0, 0, 0), null); ExampleB exampleB = new ExampleB(2.0f, "testB", new DateTime(1999, 12, 1, 0, 0, 0), null); ExampleC exampleC = new ExampleC(3.0f, "testC", new DateTime(2000, 12, 1, 0, 0, 0), null); exampleA.Reference = exampleB; exampleB.Reference = exampleC; exampleC.Reference = exampleA; while (walk) { Console.WriteLine("\nWhich class from test one you want to serialize, ExampleA [a], ExampleB [b], ExampleC [c]: \n"); char example = Console.ReadKey().KeyChar; switch (example) { case 'a': using (Stream stream = new FileStream("Own Serialization Example A.txt", FileMode.Create)) { formatter.Serialize(stream, exampleA); } walk = false; break; case 'b': using (Stream stream = new FileStream("Own Serialization Example B.txt", FileMode.Create)) { formatter.Serialize(stream, exampleB); } walk = false; break; case 'c': using (Stream stream = new FileStream("Own Serialization Example C.txt", FileMode.Create)) { formatter.Serialize(stream, exampleC); } walk = false; break; default: Console.WriteLine("\nInvalid choice of clas for example."); break; } } run = false; break; case 'd': ExampleA exampleA_Des = null; ExampleB exampleB_Des = null; ExampleC exampleC_Des = null; while (walk) { Console.WriteLine("\nWhich class from test one you want to serialize, ExampleA [a], ExampleB [b], ExampleC [c]: \n"); char example = Console.ReadKey().KeyChar; switch (example) { case 'a': using (Stream stream = new FileStream("Own Serialization Example A.txt", FileMode.Open)) { exampleA_Des = (ExampleA)formatter.Deserialize(stream); } if (exampleA_Des.Reference.Reference.Reference == exampleA_Des) { Console.WriteLine("\nDeserialization ended correctly."); } else { Console.WriteLine("\nSomething went wrong."); } walk = false; break; case 'b': using (Stream stream = new FileStream("Own Serialization Example B.txt", FileMode.Open)) { exampleB_Des = (ExampleB)formatter.Deserialize(stream); } if (exampleB_Des.Reference.Reference.Reference == exampleB_Des) { Console.WriteLine("\nDeserialization ended correctly."); } else { Console.WriteLine("\nSomething went wrong."); } walk = false; break; case 'c': using (Stream stream = new FileStream("Own Serialization Example C.txt", FileMode.Open)) { exampleC_Des = (ExampleC)formatter.Deserialize(stream); } if (exampleC_Des.Reference.Reference.Reference == exampleC_Des) { Console.WriteLine("\nDeserialization ended correctly."); } else { Console.WriteLine("\nSomething went wrong."); } walk = false; break; default: Console.WriteLine("\nInvalid choice of clas for example."); break; } } run = false; break; default: Console.WriteLine("\nInvalid choice."); break; } } } else if (format == Format.XML) { bool run = true; bool walk = true; while (run) { Console.WriteLine("\nDo you want to serialize [s] or deserialize [d]: \n"); char choice = Console.ReadKey().KeyChar; switch (choice) { case 's': ExampleXmlA exampleA = new ExampleXmlA(1.0f, "testA", new DateTime(1998, 12, 1, 0, 0, 0), null); ExampleXmlB exampleB = new ExampleXmlB(2.0f, "testB", new DateTime(1999, 12, 1, 0, 0, 0), null); ExampleXmlC exampleC = new ExampleXmlC(3.0f, "testC", new DateTime(2000, 12, 1, 0, 0, 0), null); exampleA.Reference = exampleB; exampleB.Reference = exampleC; exampleC.Reference = exampleA; while (walk) { Console.WriteLine("\nWhich class from test one you want to serialize, ExampleA [a], ExampleB [b], ExampleC [c]: "); char example = Console.ReadKey().KeyChar; switch (example) { case 'a': XmlSerializationHelper <ExampleXmlA> .Serialize("Xml Serialization Example A.txt", exampleA); walk = false; break; case 'b': XmlSerializationHelper <ExampleXmlB> .Serialize("Xml Serialization Example B.txt", exampleB); walk = false; break; case 'c': XmlSerializationHelper <ExampleXmlC> .Serialize("Xml Serialization Example C.txt", exampleC); walk = false; break; default: Console.WriteLine("\nInvalid choice of clas for example."); break; } } run = false; break; case 'd': ExampleXmlA exampleA_Des = null; ExampleXmlB exampleB_Des = null; ExampleXmlC exampleC_Des = null; while (walk) { Console.WriteLine("\nWhich class from test one you want to serialize, ExampleA [a], ExampleB [b], ExampleC [c]: "); char example = Console.ReadKey().KeyChar; switch (example) { case 'a': exampleA_Des = XmlSerializationHelper <ExampleXmlA> .Deserilize("Xml Serialization Example A.txt"); if (exampleA_Des.Reference.Reference.Reference == exampleA_Des) { Console.WriteLine("\nDeserialization ended correctly."); } else { Console.WriteLine("\nSomething went wrong."); } walk = false; break; case 'b': exampleB_Des = XmlSerializationHelper <ExampleXmlB> .Deserilize("Xml Serialization Example B.txt"); if (exampleB_Des.Reference.Reference.Reference == exampleB_Des) { Console.WriteLine("\nDeserialization ended correctly."); } else { Console.WriteLine("\nSomething went wrong."); } walk = false; break; case 'c': exampleC_Des = XmlSerializationHelper <ExampleXmlC> .Deserilize("Xml Serialization Example C.txt"); if (exampleC_Des.Reference.Reference.Reference == exampleC_Des) { Console.WriteLine("\nDeserialization ended correctly."); } else { Console.WriteLine("\nSomething went wrong."); } walk = false; break; default: Console.WriteLine("\nInvalid choice of clas for example."); break; } } run = false; break; default: Console.WriteLine("\nInvalid choice."); break; } } } else { Console.WriteLine("\nWrong format was choosen."); } }