dotnet/runtime

MetadataReader API for getting content of embedded resources

Open

#23,234 建立於 2017年8月17日

在 GitHub 查看
 (17 留言) (4 反應) (0 負責人)C# (5,445 fork)batch import
api-needs-workarea-System.Reflection.Metadatahelp wanted

倉庫指標

Star
 (17,886 star)
PR 合併指標
 (平均合併 12天 11小時) (30 天內合併 661 個 PR)

描述

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

貢獻者指南