コード例 #1
0
        private void PerformSerializationVersionsTestOnField(FieldInfo field, Type type)
        {
            var versionNumbers = field.FieldType.FullName == "System.Byte[]" ? (byte[])field.GetValue(null) : null;

            if (versionNumbers == null)
            {
                // This is not a VERSION_NUMBERS field, move along
                return;
            }

            // Test all the defined version numbers may be serialised and deserialised
            foreach (var versionNumber in versionNumbers)
            {
                var writer = new TestBinaryWriter();
                writer.WriteByte(versionNumber);
                var reader = new TestBinaryReader(writer._stream.BaseStream as MemoryStream);

                var item = Activator.CreateInstance(type) as IBinarizable;

                item.Should().NotBeNull();

                Action act = () => item.ReadBinary(reader);
                act.Should().NotThrow <TRexSerializationVersionException>();
            }
        }
コード例 #2
0
        private void PerformSerializationVersionTestOnField(FieldInfo field, Type type)
        {
            uint versionNumber = field.FieldType.FullName == "System.Byte" ? (byte)field.GetValue(null)
        : field.FieldType.FullName == "System.UInt32" ? (ushort)field.GetValue(null)
        : field.FieldType.FullName == "System.UInt16" ? (uint)field.GetValue(null)
        : uint.MaxValue;

            if (versionNumber == uint.MaxValue)
            {
                // This is not a VERSION_NUMBER field, move along
                return;
            }

            var expectedVersions = new[] { versionNumber };

            var writer = new TestBinaryWriter();

            writer.WriteByte((byte)(versionNumber + 1));
            var reader = new TestBinaryReader(writer._stream.BaseStream as MemoryStream);

            var item = Activator.CreateInstance(type);

            item.Should().NotBeNull();

            Action act = null;

            if (item is IVersionCheckedBinarizableSerializationBase versionCheckedItem)
            {
                act = () => versionCheckedItem.InternalFromBinary(reader);
            }
            else if (item is IBinarizable binarizableItem)
            {
                act = () => binarizableItem.ReadBinary(reader);
            }

            act.Should().Throw <TRexSerializationVersionException>().WithMessage("Invalid version read during deserialization*"); //TRexSerializationVersionException.ErrorMessage(expectedVersions, versionNumber + 1));
        }