Esempio n. 1
0
 public InterfaceReadOnlyCollectionFormatter()
 {
     this.formatter       = Formatter <T> .Default;
     this.formatterLength = formatter.GetLength();
 }
Esempio n. 2
0
        public override int?GetLength()
        {
            var len = innerFormatter.GetLength();

            return((len == null) ? null : len + 1);
        }
Esempio n. 3
0
 public InterfaceSetFormatter()
 {
     this.formatter       = Formatter <T> .Default;
     this.formatterLength = formatter.GetLength();
 }
Esempio n. 4
0
 public ArrayFormatter()
 {
     this.formatter       = Formatter <T> .Default;
     this.formatterLength = formatter.GetLength();
 }
Esempio n. 5
0
 public CollectionFormatter()
 {
     this.formatter       = Formatter <TElement> .Default;
     this.formatterLength = formatter.GetLength();
     this.isList          = typeof(TCollection).GetGenericTypeDefinition() == typeof(List <>);
 }
 public InterfaceCollectionFormatter()
 {
     this.formatter       = Formatter <TTypeResolver, T> .Default;
     this.formatterLength = formatter.GetLength();
 }
Esempio n. 7
0
        public override int Serialize(ref byte[] bytes, int offset, IList <T> value)
        {
            if (value == null)
            {
                BinaryUtil.WriteInt32(ref bytes, offset, -1);
                return(4);
            }

            var segment = value as IZeroFormatterSegment;

            if (segment != null)
            {
                return(segment.Serialize(ref bytes, offset));
            }

            var length = formatter.GetLength();

            if (length != null)
            {
                // FixedSize Array
                var writeSize = value.Count * length.Value + 4;
                if (bytes == null)
                {
                    bytes = new byte[writeSize];
                }

                offset += BinaryUtil.WriteInt32(ref bytes, offset, value.Count);

                // Optimize iteration
                var array = value as T[];
                if (array != null)
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        offset += formatter.Serialize(ref bytes, offset, array[i]);
                    }
                }
                else
                {
                    var list = value as List <T>;
                    if (list != null)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            offset += formatter.Serialize(ref bytes, offset, list[i]);
                        }
                    }
                    else
                    {
                        foreach (var item in value)
                        {
                            offset += formatter.Serialize(ref bytes, offset, item);
                        }
                    }
                }

                return(writeSize);
            }
            else
            {
                var startoffset      = offset;
                var indexStartOffset = startoffset + 8;
                offset = (startoffset + 8) + (value.Count * 4);

                // Optimize iteration
                var array = value as T[];
                if (array != null)
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        var size = formatter.Serialize(ref bytes, offset, array[i]);
                        BinaryUtil.WriteInt32Unsafe(ref bytes, indexStartOffset + i * 4, offset - startoffset);
                        offset += size;
                    }
                }
                else
                {
                    var list = value as List <T>;
                    if (list != null)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            var size = formatter.Serialize(ref bytes, offset, list[i]);
                            BinaryUtil.WriteInt32Unsafe(ref bytes, indexStartOffset + i * 4, offset - startoffset);
                            offset += size;
                        }
                    }
                    else
                    {
                        var count = 0;
                        foreach (var item in value)
                        {
                            var size = formatter.Serialize(ref bytes, offset, item);
                            BinaryUtil.WriteInt32Unsafe(ref bytes, indexStartOffset + count * 4, offset - startoffset);
                            offset += size;
                            count++;
                        }
                    }
                }

                BinaryUtil.WriteInt32(ref bytes, startoffset + 4, value.Count);

                var totalBytes = offset - startoffset;
                BinaryUtil.WriteInt32Unsafe(ref bytes, startoffset, totalBytes);

                return(totalBytes);
            }
        }