Document 結構

定義

偵錯中繼資料中的來源文件。

public value class Document
public readonly struct Document
public struct Document
type Document = struct
Public Structure Document
繼承
Document

範例

此範例示範如何在可攜式 PDB 檔案中顯示原始檔案的相關資訊:

static string ReadDocumentPath(MetadataReader reader, Document doc)
{
    BlobReader blob = reader.GetBlobReader(doc.Name);

    // Read path separator character
    var separator = (char)blob.ReadByte();
    var sb = new StringBuilder(blob.Length * 2);

    // Read path segments
    while (true)
    {
        BlobHandle bh = blob.ReadBlobHandle();

        if (!bh.IsNil)
        {
            byte[] nameBytes = reader.GetBlobBytes(bh);
            sb.Append(Encoding.UTF8.GetString(nameBytes));
        }

        if (blob.Offset >= blob.Length) break;

        sb.Append(separator);
    }

    return sb.ToString();
}

public static void ReadPdbDocuments(string pdbPath)
{
    // Open Portable PDB file
    using var fs = new FileStream(pdbPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    using MetadataReaderProvider provider = MetadataReaderProvider.FromPortablePdbStream(fs);
    
    MetadataReader reader = provider.GetMetadataReader();

    // Display information about documents in each MethodDebugInformation table entry
    foreach (MethodDebugInformationHandle h in reader.MethodDebugInformation)
    {
        MethodDebugInformation mdi = reader.GetMethodDebugInformation(h);

        if (mdi.Document.IsNil) continue;

        int token = MetadataTokens.GetToken(h);
        Console.WriteLine($"MethodDebugInformation 0x{token.ToString("X")}");

        Document doc = reader.GetDocument(mdi.Document);
        Console.WriteLine($"File: {ReadDocumentPath(reader, doc)}");
        Guid guidLang = reader.GetGuid(doc.Language);
        Console.WriteLine($"Language: {guidLang}");
        Guid guidHashAlg = reader.GetGuid(doc.HashAlgorithm);
        Console.WriteLine($"Hash algorithm: {guidHashAlg}");
        Console.WriteLine();
    }
}

備註

如需詳細資訊,請參閱 可攜式 PDB v1.0:格式規格

屬性

Hash

取得文件內容雜湊。

HashAlgorithm

取得用來計算 Hash (SHA1、SHA256 等) 的雜湊演算法。

Language

取得原始程式碼語言 (C#、VB、F# 等)。

Name

取得文件名稱 Blob。

適用於