public T ReadMetadata <T>() { using (var reader = CreateReader()) { var header = new MetadataHeader(reader); //MetadataHeader self registers to reader using (var vreader = (DependencyReader)reader.CreateVirtualReader(header.Header.HeaderSize)) { var mainBlock = header.StructureDefinitions.First(s => s.Type == StructureType.Main).TargetIndex; vreader.Seek(header.DataBlocks[mainBlock].Offset, SeekOrigin.Begin); var result = vreader.ReadObject <T>(); var blockProps = typeof(T).GetProperties() .Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(BlockCollection <>)); foreach (var prop in blockProps) { var collection = prop.GetValue(result) as IBlockCollection; var offset = OffsetAttribute.ValueFor(prop); collection.LoadBlocks(mainBlock, offset, vreader); } return(result); } } }
public void TestOffsetValueExpression() { Assert.AreEqual(0, OffsetAttribute.ValueFor((DataClass01 obj) => obj.Property1)); Assert.AreEqual(10, OffsetAttribute.ValueFor((DataClass01 obj) => obj.Property2, 15)); Assert.AreEqual(15, OffsetAttribute.ValueFor((DataClass01 obj) => obj.Property2, 25)); Assert.AreEqual(20, OffsetAttribute.ValueFor((DataClass01 obj) => obj.Property3, 15)); }
public void TestOffsetValue() { var prop1 = typeof(DataClass01).GetProperty(nameof(DataClass01.Property1)); var prop2 = typeof(DataClass01).GetProperty(nameof(DataClass01.Property2)); var prop3 = typeof(DataClass01).GetProperty(nameof(DataClass01.Property3)); Assert.AreEqual(0, OffsetAttribute.ValueFor(prop1)); Assert.AreEqual(10, OffsetAttribute.ValueFor(prop2, 15)); Assert.AreEqual(15, OffsetAttribute.ValueFor(prop2, 25)); Assert.AreEqual(20, OffsetAttribute.ValueFor(prop3, 15)); }
internal void LoadBlocks(int currentBlock, long collectionOffset, DependencyReader reader) { if (blockCount == 0) { return; } var structdef = metadata.StructureDefinitions.First(s => s.FieldBlock == currentBlock && s.FieldOffset == collectionOffset); if (structdef.TargetIndex < 0) { return; } var block = metadata.DataBlocks[structdef.TargetIndex]; var blockSize = FixedSizeAttribute.ValueFor(typeof(T)); reader.Seek(block.Offset, SeekOrigin.Begin); for (int i = 0; i < blockCount; i++) { Add(reader.ReadObject <T>()); } var blockProps = typeof(T).GetProperties() .Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(BlockCollection <>)); int index = 0; foreach (var item in this) { var adjustedBase = blockSize * index++; foreach (var prop in blockProps) { var collection = prop.GetValue(item) as IBlockCollection; var offset = OffsetAttribute.ValueFor(prop); collection.LoadBlocks(structdef.TargetIndex, adjustedBase + offset, reader); } } }