Пример #1
0
        /// <summary>
        /// Serializes a property.
        /// </summary>
        /// <param name="context">The serialization context to use.</param>
        /// <param name="tagStream">The stream to write completed blocks of tag data to.</param>
        /// <param name="block">The temporary block to write incomplete tag data to.</param>
        /// <param name="instance">The object that the property belongs to.</param>
        /// <param name="structInfo">The structure information.</param>
        /// <param name="property">The property.</param>
        /// <param name="baseOffset">The base offset of the structure from the start of its block.</param>
        /// <exception cref="System.InvalidOperationException">Offset for property \ + property.Name + \ is outside of its structure</exception>
        private static void SerializeProperty(ISerializationContext context, MemoryStream tagStream, IDataBlock block, object instance, TagStructureAttribute structInfo, PropertyInfo property, long baseOffset)
        {
            // Get the property's TagValueAttribute
            var valueInfo = property.GetCustomAttributes(typeof(TagElementAttribute), false).FirstOrDefault() as TagElementAttribute;

            if (valueInfo == null)
            {
                return;                 // Ignore the property
            }
            if (valueInfo.Offset >= structInfo.Size)
            {
                throw new InvalidOperationException("Offset for property \"" + property.Name + "\" is outside of its structure");
            }

            // Seek to the value if it has an offset specified and write it
            if (valueInfo.Offset >= 0)
            {
                block.Stream.Position = baseOffset + valueInfo.Offset;
            }
            var startOffset = block.Stream.Position;
            var val         = property.GetValue(instance);

            SerializeValue(context, tagStream, block, val, valueInfo, property.PropertyType);
            if (valueInfo.Size > 0)
            {
                block.Stream.Position = startOffset + valueInfo.Size;
            }
        }
Пример #2
0
        /// <summary>
        /// Deserializes a property of a structure.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="context">The serialization context to use.</param>
        /// <param name="instance">The instance to store the property to.</param>
        /// <param name="structInfo">The structure information.</param>
        /// <param name="property">The property.</param>
        /// <param name="baseOffset">The offset of the start of the structure.</param>
        /// <exception cref="System.InvalidOperationException">Offset for property is outside of its structure</exception>
        private static void DeserializeProperty(BinaryReader reader, ISerializationContext context, object instance, TagStructureAttribute structInfo, PropertyInfo property, long baseOffset)
        {
            // Get the property's TagValueAttribute
            var valueInfo = property.GetCustomAttributes(typeof(TagElementAttribute), false).FirstOrDefault() as TagElementAttribute;

            if (valueInfo == null)
            {
                return;                 // Ignore the property
            }
            if (valueInfo.Offset >= structInfo.Size)
            {
                throw new InvalidOperationException("Offset for property \"" + property.Name + "\" is outside of its structure");
            }

            // Seek to the value if it has an offset specified and then read it
            if (valueInfo.Offset >= 0)
            {
                reader.BaseStream.Position = baseOffset + valueInfo.Offset;
            }
            var startOffset = reader.BaseStream.Position;

            property.SetValue(instance, DeserializeValue(reader, context, valueInfo, property.PropertyType));
            if (valueInfo.Size > 0)
            {
                reader.BaseStream.Position = startOffset + valueInfo.Size;                 // Honor the value's size if it has one set
            }
        }