Esempio n. 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());
                }
            }
        }
Esempio n. 2
0
 public static bool TryGetBinaryConverter(Type typeToSerialize, out BinaryConverterBase result)
 {
     if (typeToSerialize == null)
     {
         throw new NullReferenceException($"The parameter '{nameof(typeToSerialize)}' can't be null.");
     }
     return(SerializationPlansCachedByEveryTypeHachcode.TryGetValue(typeToSerialize.GetHashCode(), out result));
 }
Esempio n. 3
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));
        }
Esempio n. 4
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);
         }
     }
 }
Esempio n. 5
0
        public static void SetSerializePlan(Type typeToSerialize, BinaryConverterBase value)
        {
            BinaryConverterBase result;

            if (!SerializationPlansCachedByEveryTypeHachcode.TryGetValue(typeToSerialize.GetHashCode(), out result))
            {
                if (typeToSerialize == null)
                {
                    throw new NullReferenceException($"The parameter '{nameof(typeToSerialize)}' can't be null.");
                }

                lock (SerializationPlansCachedByEveryTypeHachcode_Copy)
                {
                    if (!SerializationPlansCachedByEveryTypeHachcode_Copy.ContainsKey(typeToSerialize.GetHashCode()))
                    {
                        SerializationPlansCachedByEveryTypeHachcode_Copy[typeToSerialize.GetHashCode()] = value;
                        SerializationPlansCachedByEveryTypeHachcode = SerializationPlansCachedByEveryTypeHachcode_Copy.ToDictionary(item => item.Key, item => item.Value);
                    }
                }
            }
        }