Search code examples
c#clr

How to access .Net CLR blob items


I'm playing around with Roslyn, and so far I've learned that a C# string literal is placed in the CLR #US (User String) heap using System.Reflection.Metadata, specifically using the GetOrAddUserString() method. Then at runtime these strings are accessed via the ldstr IL instruction.

I can see that System.Reflection.Metadata also contains a GetOrAddConstantBlob() method that can place data (including UTF16 strings) on a blob heap. That's presumably the #Blob metadata that can be seen with dotPeek, as can be seen in a screenshot in this blog article: https://blog.maartenballiauw.be/post/2016/11/15/exploring-memory-allocation-and-strings.html

My question is, how can one access this blob data? Is there some IL instruction corresponding to ldstr that does it? Some method in System.Reflection.Metadata? Is there any documentation about this? (I have tried Googling all sorts of keywords, but without any luck.)


Solution

  • There is no IL instruction that accesses data from the #Blob stream. Instead, blobs are referenced by fields in the metadata tables. For example, the Signature column of the MemberRef metadata table is offset of the member signature in the #Blob stream.

    The file format (including the #Blob stream, IL instructions and metadata tables) is defined by ECMA-335 (partitions II and III in particular).