Beispiel #1
0
        public static byte[] Serialize(object obj)
        {
            //  Core.Serialization.Utility.Debuging.WriteLine($"Start Binary Serializaing at {DateTime.Now} ...");
            //   Console.WriteLine($"Start Binary Serializaing at {DateTime.Now} ...");
            var context = new SerializationContext()
            {
                IsFirstItem = true
            };

            using (var memoryStream = new MemoryStream())
            {
                using (var writer = new BinaryWriterCore(memoryStream))
                {
                    if (obj == null)
                    {
                        new NullBinaryConverter().Serialize(obj, writer, context);
                    }
                    else
                    {
                        var type = obj.GetType();
                        BinaryConverterBase serializationPlan;

                        serializationPlan = BinaryConverterBase.GetBinaryConverter(type);
                        serializationPlan.Serialize(obj, writer, context);
                        //        Core.Serialization.Utility.Debuging.WriteLine($"End Binary Serializaing at {DateTime.Now} ...");
                        //         Console.WriteLine($"End Binary Serializaing at {DateTime.Now} ...");
                    }
                    return(memoryStream.ToArray());
                }
            }
        }
Beispiel #2
0
        public void GetBinaryConverterByType()
        {
            var ListBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(List <int>));

            Assert.AreEqual(ListBinaryConverter.GetType(), typeof(ListBinaryConverter));

            var ListBinaryConverter1 = BinaryConverterBase.GetBinaryConverter(typeof(List <User <UserRole> >));

            Assert.AreEqual(ListBinaryConverter1.GetType(), typeof(ListBinaryConverter));

            var dicBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(Dictionary <int, string>));

            Assert.AreEqual(dicBinaryConverter.GetType(), typeof(DictionaryBinaryConverter));

            var Dic1BinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(ConcurrentDictionary <int, User <UserRole> >));

            Assert.AreEqual(Dic1BinaryConverter.GetType(), typeof(DictionaryBinaryConverter));

            var anonmousTypeBinaryConverter = BinaryConverterBase.GetBinaryConverter(new { Id = 10, Name = "ajkhjgf" }.GetType());

            Assert.AreEqual(anonmousTypeBinaryConverter.GetType(), typeof(UserDefinedTypeBinaryConverter));

            var userDefinedBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(UserDefined));

            Assert.AreEqual(userDefinedBinaryConverter.GetType(), typeof(UserDefinedTypeBinaryConverter));

            var entityBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(User <UserRole>));

            Assert.AreEqual(entityBinaryConverter.GetType(), typeof(UserDefinedTypeBinaryConverter));

            var intBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(int));

            Assert.AreEqual(intBinaryConverter.GetType(), typeof(IntBinaryConverter));

            var nulIntBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(int?));

            Assert.AreEqual(nulIntBinaryConverter.GetType(), typeof(IntBinaryConverter));

            var bytearrayBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(byte[]));

            Assert.AreEqual(bytearrayBinaryConverter.GetType(), typeof(ByteArrayBinaryConverter));

            var chararrayBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(char[]));

            Assert.AreEqual(chararrayBinaryConverter.GetType(), typeof(CharArrayBinaryConverter));

            var arrayBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(UserDefined[]));

            Assert.AreEqual(arrayBinaryConverter.GetType(), typeof(ArrayBinaryConverter));
            var userUndefinedBinaryConverter = BinaryConverterBase.GetBinaryConverter(typeof(Point));

            Assert.AreEqual(userUndefinedBinaryConverter.GetType(), typeof(UserDefinedTypeBinaryConverter));
        }
Beispiel #3
0
 public static object Deserialize(byte[] binary, Type objectType)
 {
     //   Core.Serialization.Utility.Debuging.WriteLine($"Start Binary Deserializaing at {DateTime.Now} ...");
     //  Console.WriteLine($"Start Binary Deserializaing at {DateTime.Now} ...");
     using (var memoryStream = new MemoryStream(binary))
     {
         using (var reader = new BinaryReaderCore(memoryStream))
         {
             BinaryConverterBase serializationPlan;
             var    context = new DeserializationContext();
             object obj;
             serializationPlan = BinaryConverterBase.GetBinaryConverter(objectType);
             obj = serializationPlan.Deserialize(reader, objectType, context);
             //   Core.Serialization.Utility.Debuging.WriteLine($"End Binary Deserializaing at {DateTime.Now} ...");
             //    Console.WriteLine($"End Binary Deserializaing at {DateTime.Now} ...");
             return(obj);
         }
     }
 }