Пример #1
0
        public static ArraySegment <byte> GetSegment(ArraySegment <byte> originalBytes, int index, int nextIndex, int lastIndex, DirtyTracker tracker)
        {
            var offset = GetOffset(originalBytes, index, lastIndex, tracker);

            if (offset == -1)
            {
                return(default(ArraySegment <byte>)); // note:very very dangerous.
            }

            int count;

            if (nextIndex == -1 || nextIndex > lastIndex)
            {
                count = originalBytes.Offset + originalBytes.Count - offset; // last index
            }
            else
            {
                var nextOffset = ObjectSegmentHelper.GetOffset(originalBytes, nextIndex, lastIndex, tracker);
                if (nextOffset == -1)
                {
                    return(default(ArraySegment <byte>)); // note: precondition failed.
                }
                count = nextOffset - offset;
            }
            return(new ArraySegment <byte>(originalBytes.Array, offset, count));
        }
        static PropertyTuple[] GetPropertiesWithVerify(TypeBuilder typeBuilder)
        {
            var resolverType = typeof(TTypeResolver);
            var lastIndex    = -1;

            var list = new List <PropertyTuple>();

            // getproperties contains verify.
            foreach (var p in DynamicObjectDescriptor.GetMembers(resolverType, typeof(T), true))
            {
                var propInfo = p.Item2;
                var index    = p.Item1;

                var formatter = (IFormatter)typeof(Formatter <,>).MakeGenericType(resolverType, propInfo.MemberType).GetTypeInfo().GetProperty("Default").GetValue(null, Type.EmptyTypes);

                if (formatter == null)
                {
                    throw new InvalidOperationException("Circular reference does not supported. " + typeof(T).Name + "." + propInfo.Name);
                }

                if (formatter.GetLength() == null)
                {
                    if (!ObjectSegmentHelper.IsLazySegment(propInfo.MemberType))
                    {
                        // CacheSegment
                        var fieldBuilder = typeBuilder.DefineField("<>_" + propInfo.Name, typeof(CacheSegment <,>).MakeGenericType(resolverType, propInfo.MemberType), FieldAttributes.Private);
                        list.Add(new PropertyTuple {
                            Index = index, PropertyInfo = propInfo.PropertyInfoUnsafe, IsFixedSize = false, SegmentField = fieldBuilder, IsCacheSegment = true
                        });
                    }
                    else
                    {
                        var fieldBuilder = typeBuilder.DefineField("<>_" + propInfo.Name, propInfo.MemberType, FieldAttributes.Private);
                        list.Add(new PropertyTuple {
                            Index = index, PropertyInfo = propInfo.PropertyInfoUnsafe, IsFixedSize = false, SegmentField = fieldBuilder
                        });
                    }
                }
                else
                {
                    list.Add(new PropertyTuple {
                        Index = index, PropertyInfo = propInfo.PropertyInfoUnsafe, IsFixedSize = true, FixedSize = formatter.GetLength().Value
                    });
                }

                lastIndex = index;
            }

            return(list.OrderBy(x => x.Index).ToArray());
        }
        public static T DeserializeSegment <T>(ArraySegment <byte> originalBytes, int index, int lastIndex, DirtyTracker tracker)
        {
            var offset = ObjectSegmentHelper.GetOffset(originalBytes, index, lastIndex, tracker);

            if (offset == -1)
            {
                return(default(T));
            }

            int size;
            var array = originalBytes.Array;

            return(Formatter <T> .Default.Deserialize(ref array, offset, tracker, out size));
        }
 public static void SetFixedProperty <T>(ArraySegment <byte> bytes, int index, int lastIndex, byte[] extraBytes, T value, DirtyTracker tracker)
 {
     if (index <= lastIndex)
     {
         var array = bytes.Array;
         Formatter <T> .Default.Serialize(ref array, ObjectSegmentHelper.GetOffset(bytes, index, lastIndex, tracker), value);
     }
     else
     {
         // from extraBytes
         var offset = GetExtraBytesOffset(extraBytes, lastIndex, index);
         Formatter <T> .Default.Serialize(ref extraBytes, offset, value);
     }
 }
 public static T GetFixedProperty <T>(ArraySegment <byte> bytes, int index, int lastIndex, byte[] extraBytes, DirtyTracker tracker)
 {
     if (index <= lastIndex)
     {
         var array = bytes.Array;
         int _;
         return(Formatter <T> .Default.Deserialize(ref array, ObjectSegmentHelper.GetOffset(bytes, index, lastIndex, tracker), tracker, out _));
     }
     else
     {
         // from extraBytes
         var offset = GetExtraBytesOffset(extraBytes, lastIndex, index);
         int _;
         return(Formatter <T> .Default.Deserialize(ref extraBytes, offset, tracker, out _));
     }
 }