dotnet/runtime

MetadataReader API for getting content of embedded resources

Open

#23,234 opened on Aug 17, 2017

View on GitHub
 (17 comments) (4 reactions) (0 assignees)C# (5,445 forks)batch import
api-needs-workarea-System.Reflection.Metadatahelp wanted

Repository metrics

Stars
 (17,886 stars)
PR merge metrics
 (Avg merge 12d 11h) (661 merged PRs in 30d)

Description

The metadata reader does not currently provide an easy way to get at the content of an embedded resource.

First draft API proposal

namespace System.Reflection.Metadata
{
    public static class PEReaderExtensions // existing class
    {
       // new API
       Stream GetEmbeddedResourceStream(this PEReader peReader, ManifestResource resource);
    }
}

Sample implementation

https://gist.github.com/nguerrera/6864d2a907cb07d869be5a2afed8d764

  • DISCLAIMER: I wrote that a long time ago against pre-release API and did not re-test after simply fixing compilation errors.
    • Update: @safern got it working for him after one fix he sent me that I've applied to the gist.
  • InvalidDataException should be BadImageFormatException as should any arithmetic overflow caused by bad data.
  • We can make the code safer by using BlobReader for more than just the length. It was written before there was PEMemoryBlock.GetReader() and the use of BlobReader used to be direct pointer indirection, and I just changed it locally to fix an endianness bug (at the time S.R.M threw on big endian).

See also

https://github.com/Microsoft/msbuild/pull/2254#discussion_r129891209

Open questions

#* Is Stream the right return type? That is what msbuild asked for and was also most convenient for me when I wrote that code a while ago. Should we have other overloads, PEMemoryBlock, ImmutableArray, byte[], BlobReader? We have a mix for other similar things and don't directly provide Streams. Maybe we just need ToStream() helper on PEMemoryBlock or BlobReader instead?

cc @rainersigwald @ericstj @tmat

Contributor guide