< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 91
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetMethodBody(...)0%440%
GetMetadataReader(...)100%110%
GetMetadataReader(...)100%110%
GetMetadataReader(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/PEReaderExtensions.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.ComponentModel;
 5using System.IO;
 6using System.Reflection.PortableExecutable;
 7using System.Text;
 8
 9namespace System.Reflection.Metadata
 10{
 11    // EditorBrowsable(Never) so that we don't clutter completion list with this type because a user that only has Syste
 12    // imported and has type PE is likely looking to resolve PEReader from the System.Reflection.PortableExecutable and 
 13    // these extensions as regular statics.
 14    [EditorBrowsable(EditorBrowsableState.Never)]
 15    public static class PEReaderExtensions
 16    {
 17        /// <summary>
 18        /// Returns a body block of a method with specified Relative Virtual Address (RVA);
 19        /// </summary>
 20        /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null.</exception>
 21        /// <exception cref="BadImageFormatException">The body is not found in the metadata or is invalid.</exception>
 22        /// <exception cref="InvalidOperationException">Section where the method is stored is not available.</exception>
 23        /// <exception cref="IOException">IO error while reading from the underlying stream.</exception>
 24        public static MethodBodyBlock GetMethodBody(this PEReader peReader, int relativeVirtualAddress)
 025        {
 026            if (peReader is null)
 027            {
 028                Throw.ArgumentNull(nameof(peReader));
 29            }
 30
 031            var block = peReader.GetSectionData(relativeVirtualAddress);
 032            if (block.Length == 0)
 033            {
 034                throw new BadImageFormatException(SR.Format(SR.InvalidMethodRva, relativeVirtualAddress));
 35            }
 36
 37            // Call to validating public BlobReader constructor is by design -- we need to throw PlatformNotSupported on
 038            return MethodBodyBlock.Create(block.GetReader());
 039        }
 40
 41        /// <summary>
 42        /// Gets a <see cref="MetadataReader"/> from a <see cref="PEReader"/>.
 43        /// </summary>
 44        /// <remarks>
 45        /// The caller must keep the <see cref="PEReader"/> alive and undisposed throughout the lifetime of the metadata
 46        /// </remarks>
 47        /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null</exception>
 48        /// <exception cref="PlatformNotSupportedException">The current platform is big-endian.</exception>
 49        /// <exception cref="IOException">IO error while reading from the underlying stream.</exception>
 50        public static MetadataReader GetMetadataReader(this PEReader peReader)
 051        {
 052            return GetMetadataReader(peReader, MetadataReaderOptions.ApplyWindowsRuntimeProjections, null);
 053        }
 54
 55        /// <summary>
 56        /// Gets a <see cref="MetadataReader"/> from a <see cref="PEReader"/>.
 57        /// </summary>
 58        /// <remarks>
 59        /// The caller must keep the <see cref="PEReader"/> alive and undisposed throughout the lifetime of the metadata
 60        /// </remarks>
 61        /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null</exception>
 62        /// <exception cref="PlatformNotSupportedException">The current platform is big-endian.</exception>
 63        /// <exception cref="IOException">IO error while reading from the underlying stream.</exception>
 64        public static MetadataReader GetMetadataReader(this PEReader peReader, MetadataReaderOptions options)
 065        {
 066            return GetMetadataReader(peReader, options, null);
 067        }
 68
 69        /// <summary>
 70        /// Gets a <see cref="MetadataReader"/> from a <see cref="PEReader"/>.
 71        /// </summary>
 72        /// <remarks>
 73        /// The caller must keep the <see cref="PEReader"/> undisposed throughout the lifetime of the metadata reader.
 74        /// </remarks>
 75        /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null</exception>
 76        /// <exception cref="ArgumentException">The encoding of <paramref name="utf8Decoder"/> is not <see cref="UTF8Enc
 77        /// <exception cref="PlatformNotSupportedException">The current platform is big-endian.</exception>
 78        /// <exception cref="IOException">IO error while reading from the underlying stream.</exception>
 79        public static unsafe MetadataReader GetMetadataReader(this PEReader peReader, MetadataReaderOptions options, Met
 080        {
 081            if (peReader is null)
 082            {
 083                Throw.ArgumentNull(nameof(peReader));
 84            }
 85
 086            var metadata = peReader.GetMetadata();
 087            return new MetadataReader(metadata.Pointer, metadata.Length, options, utf8Decoder, memoryOwner: peReader);
 088        }
 89    }
 90}
 91