dotnet/runtime

MetadataReader API for getting content of embedded resources

Open

#23.234 aperta il 17 ago 2017

Vedi su GitHub
 (17 commenti) (4 reazioni) (0 assegnatari)C# (5445 fork)batch import
api-needs-workarea-System.Reflection.Metadatahelp wanted

Metriche repository

Star
 (17.886 star)
Metriche merge PR
 (Merge medio 12g 11h) (661 PR mergiate in 30 g)

Descrizione

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

Guida contributor