public void ProcessReferenceType <T>(IWriteOnlyExactValueDescriptor <T> descriptor)
            where T : class
        {
            Type type;

            if (TryReadReferenceType(descriptor, out type))
            {
                // Do not create instance when predefined type, Just route to it.
                if (BinaryReaderWriterExtensions.IsPredefinedType(type))
                {
                    descriptor.Route(
                        new BinaryStateMember
                            (new MyCustomStateMember((IReflectionStateMember)descriptor.StateMember,
                                                     type),
                            true,
                            true),
                        descriptor.Owner,
                        true);

                    return;
                }

                T instance = (T)Activator.CreateInstance(type);
                _objects.Add(instance);
                descriptor.Set(instance);
                descriptor.Climb();
            }
        }
        public void ProcessForWriteOnly(IWriteOnlyExactValueDescriptor <DateTime> descriptor)
        {
            AssertKnownType(descriptor);
            long ticks = _reader.ReadInt64();

            descriptor.Set(DateTime.FromBinary(ticks));
        }
Exemplo n.º 3
0
        public void ProcessString(IWriteOnlyExactValueDescriptor <string> descriptor)
        {
            XElement element =
                _reader.Element(descriptor.StateMember.Name);

            string result = element.Value;

            descriptor.Set(result);
        }
        public void ProcessPrimitives <[Primitive] T>(IWriteOnlyExactValueDescriptor <T> descriptor)
        {
            T value;

            if (_store.TryGet <T>(descriptor.StateMember.Name, out value))
            {
                descriptor.Set(value);
            }
        }
Exemplo n.º 5
0
        public void ProcessInt32(IWriteOnlyExactValueDescriptor <int> descriptor)
        {
            XElement element =
                _reader.Element(descriptor.StateMember.Name);

            int result =
                Convert.ToInt32(element.Value);

            descriptor.Set(result);
        }
        public void ProcessForWriteOnly(IWriteOnlyExactValueDescriptor <string> descriptor)
        {
            Type type;

            // Yeah yeah yeah, string is also a reference type.
            if (TryReadReferenceType(descriptor, out type))
            {
                string value = _reader.ReadString();
                _objects.Add(value);
                descriptor.Set(value);
            }
        }
        private bool TryReadReferenceType <T>(IWriteOnlyExactValueDescriptor <T> descriptor, out Type type)
            where T : class
        {
            BinaryStateMember member = (BinaryStateMember)descriptor.StateMember;

            type = member.RuntimeType;

            if (member.HeaderHandled)
            {
                return(true);
            }

            byte header = _reader.ReadByte();

            if (header == ReadWriteHeader.Null)
            {
                descriptor.Set(null);
            }
            else if (header == ReadWriteHeader.Revisited)
            {
                HandleVisited(descriptor);
            }
            else
            {
                if (!member.KnownType &&
                    (header == ReadWriteHeader.UnknownType))
                {
                    string assemblyQualifiedName = _reader.ReadString();

                    type = Type.GetType(assemblyQualifiedName);

                    return(true);
                }

                if (header == ReadWriteHeader.KnownType)
                {
                    return(true);
                }

                throw new Exception(
                          "Read an unknown header - probably a mismatch between reader and writer - i.e: a bug :(");
            }

            return(false);
        }
        public void ProcessArray <[IsArray] TArray>(IWriteOnlyExactValueDescriptor <TArray> descriptor)
            where TArray : class
        {
            // TODO: a bit too much code duplication here.
            Type type;

            if (TryReadReferenceType(descriptor, out type))
            {
                int[] lowerIndices = ReadIntArray();
                int[] lengths      = ReadIntArray();
                Type  elementType  = typeof(TArray).GetElementType();

                TArray value;

                if (type == elementType.MakeArrayType())
                {
                    // avoid T[*].
                    int length = lengths[0];

                    value =
                        (TArray)(object)
                        Array.CreateInstance(elementType, length);
                }
                else
                {
                    value =
                        (TArray)(object)Array.CreateInstance
                            (elementType,
                            lengths,
                            lowerIndices);
                }

                _objects.Add(value);

                descriptor.Set(value);

                descriptor.Climb();
            }
        }
 public void ProcessForWriteOnly(IWriteOnlyExactValueDescriptor <double> descriptor)
 {
     AssertKnownType(descriptor);
     descriptor.Set(_reader.ReadDouble());
 }
 public void ProcessForWriteOnly(IWriteOnlyExactValueDescriptor <ulong> descriptor)
 {
     AssertKnownType(descriptor);
     descriptor.Set(_reader.ReadUInt64());
 }
 public void ProcessForWriteOnly(IWriteOnlyExactValueDescriptor <short> descriptor)
 {
     AssertKnownType(descriptor);
     descriptor.Set(_reader.ReadInt16());
 }
        private void HandleVisited <T>(IWriteOnlyExactValueDescriptor <T> descriptor)
        {
            int index = _reader.ReadInt32();

            descriptor.Set((T)_objects[index]);
        }