public static uint Serialize<T>(string path, IByteSerializable<T> serializableObject)
 {
     byte[] src = serializableObject.ToBytes();
     uint hash = Crc32.ComputeHash(src);
     File.WriteAllBytes(path, src);
     return hash;
 }
Пример #2
0
        /// <summary>
        /// Serialize object to byte array
        /// </summary>
        /// <param name="instance">Instance to serialize</param>
        /// <returns>Bytes containing the object properties</returns>
        public static byte[] Serialize(IByteSerializable instance)
        {
            var index = 0;
            var bytes = new byte[instance.Size];

            instance.ToBytes(bytes, ref index);
            return(bytes);
        }
Пример #3
0
        private static BinaryNode CreateHierarchy(List <IByteSerializable> traversed, IByteSerializable obj)
        {
            if (traversed.Contains(obj))
            {
                return(null);
            }

            traversed.Add(obj);

            var node       = new BinaryNode(obj);
            var childNodes = new List <BinaryNode>();

            var children = obj
                           .GetType()
                           .GetProperties()
                           .Where(x => !x.PropertyType.IsPrimitive)
                           .Select(x => x.GetValue(obj))
                           .Where(x => x != null);

            //.OfType<IByteSerializable>()
            //.ToArray();

            foreach (var c in children)
            {
                IByteSerializable bs = c as IByteSerializable;

                if (bs != null)
                {
                    childNodes.Add(CreateHierarchy(traversed, bs));
                }

                var items = c as IEnumerable <IByteSerializable>;

                if (items != null)
                {
                    childNodes.AddRange(
                        items
                        .Where(x => x != null)
                        .Select(x => CreateHierarchy(traversed, x)));
                }
            }

            node.Children = childNodes;

            return(node);
        }
Пример #4
0
 public BinaryNode(IByteSerializable structure)
     : this()
 {
     Structure = structure;
     var order = 0;
     Fields = structure
         .GetType()
         .GetProperties()
         .Where<ByteFieldAttribute>()
         .OrderBy(x => x.Attribute.Order)
         .Select(x => new BinaryNameValuePair
         {
             Order = ++order,
             Name = x.Property.Name,
             Type = x.Property.PropertyType.Name.SubstringAtIndexOf('.', 1),
             Value = x.Property.GetValue(structure),
         })
         .ToArray();
 }
Пример #5
0
        public BinaryNode(IByteSerializable structure)
            : this()
        {
            Structure = structure;
            var order = 0;

            Fields = structure
                     .GetType()
                     .GetProperties()
                     .Where <ByteFieldAttribute>()
                     .OrderBy(x => x.Attribute.Order)
                     .Select(x => new BinaryNameValuePair
            {
                Order = ++order,
                Name  = x.Property.Name,
                Type  = x.Property.PropertyType.Name.SubstringAtIndexOf('.', 1),
                Value = x.Property.GetValue(structure),
            })
                     .ToArray();
        }
 public static void SerializeObjectFromBytes(this IByteSerializable objectToProcess, byte[] fromBytes)
 {
     var someObjectData = objectToProcess.serializationType.Serialize(fromBytes);
 }
 public static byte[] DeserializeObjectIntoBytes(this IByteSerializable objectToProcess)
 {
     return(objectToProcess.serializationType.Deserialize(objectToProcess));
 }
Пример #8
0
 public BinaryNode(IByteSerializable structure, IEnumerable <BinaryNode> children)
     : this(structure)
 {
     Children = children;
 }
Пример #9
0
 public static BinaryNode CreateHierarchy(IByteSerializable obj)
 {
     return(CreateHierarchy(new List <IByteSerializable>(), obj));
 }
Пример #10
0
 public BinaryNode(IByteSerializable structure, IEnumerable<BinaryNode> children)
     : this(structure)
 {
     Children = children;
 }
Пример #11
0
        private static BinaryNode CreateHierarchy(List<IByteSerializable> traversed, IByteSerializable obj)
        {
            if (traversed.Contains(obj))
            {
                return null;
            }

            traversed.Add(obj);

            var node = new BinaryNode(obj);
            var childNodes = new List<BinaryNode>();

            var children = obj
                .GetType()
                .GetProperties()
                .Where(x => !x.PropertyType.IsPrimitive)
                .Select(x => x.GetValue(obj))
                .Where(x => x != null);
            //.OfType<IByteSerializable>()
            //.ToArray();

            foreach (var c in children)
            {
                IByteSerializable bs = c as IByteSerializable;

                if (bs != null)
                {
                    childNodes.Add(CreateHierarchy(traversed, bs));
                }

                var items = c as IEnumerable<IByteSerializable>;

                if (items != null)
                {
                    childNodes.AddRange(
                        items
                            .Where(x => x != null)
                            .Select(x => CreateHierarchy(traversed, x)));
                }
            }

            node.Children = childNodes;

            return node;
        }
Пример #12
0
 public static BinaryNode CreateHierarchy(IByteSerializable obj)
 {
     return CreateHierarchy(new List<IByteSerializable>(), obj);
 }