예제 #1
0
        private bool WritePropertyNameBytesIfWantoSerialiseField(PropertyInfo property)
        {
            if (_encounteredProperties.TryGetValue(property, out var cachedData))
            {
                // If we've encountered this property before then we return the bytes for the Name Reference ID only (unless we've got a null value, which means skip it and
                // return null from here)
                if (cachedData == null)
                {
                    return(false);
                }

                WriteByte((byte)BinarySerialisationDataType.FieldName);
                WriteBytes(cachedData.OnlyAsReferenceID);
                return(true);
            }

            // If we haven't encountered this field before then we'll need to write out the full string data (if another write has encountered this field then the call to the
            // BinarySerialisationWriterCachedNames method should be very cheap but we need to include the string data so that the reader knows what string value to use for
            // this field - if we run into it again in this serialisation process then we'll emit a Name Reference ID and NOT the full string data)
            cachedData = BinarySerialisationWriterCachedNames.GetFieldNameBytesIfWantoSerialiseProperty(property);
            _encounteredProperties[property] = cachedData;
            if (cachedData == null)
            {
                return(false);
            }

            WriteByte((byte)BinarySerialisationDataType.FieldName);
            WriteBytes(cachedData.AsStringAndReferenceID);
            return(true);
        }
예제 #2
0
        private void WriteTypeName(Type typeIfValueIsNotNull)
        {
            // When recording a type name, either write a null string for it OR write a string and then the Name Reference ID that that string should be stored as OR write just
            // the Name Reference ID (if the type name has already been recorded once and may be reused)
            if (typeIfValueIsNotNull == null)
            {
                WriteByte((byte)BinarySerialisationDataType.String);
                StringWithoutDataType(null);
                return;
            }

            if (_recordedTypeNames.TryGetValue(typeIfValueIsNotNull, out var cachedData))
            {
                // If we've encountered this field before then we return the bytes for the Name Reference ID only
                WriteBytes(cachedData.OnlyAsReferenceID);
                return;
            }

            // If we haven't encountered this type before then we'll need to write out the full string data (if another write has encountered this type then the call to the
            // BinarySerialisationWriterCachedNames method should be very cheap but we need to include the string data so that the reader knows what string value to use for
            // this type - if we run into it again in this serialisation process then we'll emit a Name Reference ID and NOT the full string data)
            cachedData = BinarySerialisationWriterCachedNames.GetTypeNameBytes(typeIfValueIsNotNull);
            _recordedTypeNames[typeIfValueIsNotNull] = cachedData;
            WriteBytes(cachedData.AsStringAndReferenceID);
        }
예제 #3
0
        /// <summary>
        /// This should only be called when writing out data for deferred-initialised object references - otherwise the boolean return value from the FieldName method will indicate
        /// whether a property should be serialised or not
        /// </summary>
        public bool ShouldSerialiseProperty(PropertyInfo property, Type serialisationTargetType)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }
            if (serialisationTargetType == null)
            {
                throw new ArgumentNullException(nameof(serialisationTargetType));
            }

            var propertyOnType = Tuple.Create((MemberInfo)property, serialisationTargetType);

            if (_shouldSerialiseMemberCache.TryGetValue(propertyOnType, out var cachedData))
            {
                return(cachedData);
            }

            cachedData = (BinarySerialisationWriterCachedNames.GetFieldNameBytesIfWantoSerialiseProperty(property) != null);
            _shouldSerialiseMemberCache[propertyOnType] = cachedData;
            return(cachedData);
        }
예제 #4
0
        /// <summary>
        /// This should only be called when writing out data for deferred-initialised object references - otherwise the boolean return value from the FieldName method will indicate
        /// whether a field should be serialised or not
        /// </summary>
        public bool ShouldSerialiseField(FieldInfo field, Type serialisationTargetType)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }
            if (serialisationTargetType == null)
            {
                throw new ArgumentNullException(nameof(serialisationTargetType));
            }

            var fieldOnType = Tuple.Create((MemberInfo)field, serialisationTargetType);

            if (_shouldSerialiseMemberCache.TryGetValue(fieldOnType, out var cachedData))
            {
                return(cachedData);
            }

            cachedData = (BinarySerialisationWriterCachedNames.GetFieldNameBytesIfWantoSerialiseField(field, serialisationTargetType) != null);
            _shouldSerialiseMemberCache[fieldOnType] = cachedData;
            return(cachedData);
        }