MetadataBuilder 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
MetadataBuilder 類別會以高效能的方式寫入元件的元數據。 其設計目的是要供編譯器和其他組件產生工具使用。
public ref class MetadataBuilder sealed
public sealed class MetadataBuilder
type MetadataBuilder = class
Public NotInheritable Class MetadataBuilder
- 繼承
-
MetadataBuilder
範例
這個範例示範如何使用 發出主控台應用程式元件 MetadataBuilder:
private static readonly Guid s_guid = new Guid("87D4DBE1-1143-4FAD-AAB3-1001F92068E6");
private static readonly BlobContentId s_contentId = new BlobContentId(s_guid, 0x04030201);
private static MethodDefinitionHandle EmitHelloWorld(MetadataBuilder metadata, BlobBuilder ilBuilder)
{
// Create module and assembly for a console application.
metadata.AddModule(
0,
metadata.GetOrAddString("ConsoleApplication.exe"),
metadata.GetOrAddGuid(s_guid),
default(GuidHandle),
default(GuidHandle));
metadata.AddAssembly(
metadata.GetOrAddString("ConsoleApplication"),
version: new Version(1, 0, 0, 0),
culture: default(StringHandle),
publicKey: default(BlobHandle),
flags: 0,
hashAlgorithm: AssemblyHashAlgorithm.None);
// Create references to System.Object and System.Console types.
AssemblyReferenceHandle mscorlibAssemblyRef = metadata.AddAssemblyReference(
name: metadata.GetOrAddString("mscorlib"),
version: new Version(4, 0, 0, 0),
culture: default(StringHandle),
publicKeyOrToken: metadata.GetOrAddBlob(
new byte[] { 0xB7, 0x7A, 0x5C, 0x56, 0x19, 0x34, 0xE0, 0x89 }
),
flags: default(AssemblyFlags),
hashValue: default(BlobHandle));
TypeReferenceHandle systemObjectTypeRef = metadata.AddTypeReference(
mscorlibAssemblyRef,
metadata.GetOrAddString("System"),
metadata.GetOrAddString("Object"));
TypeReferenceHandle systemConsoleTypeRefHandle = metadata.AddTypeReference(
mscorlibAssemblyRef,
metadata.GetOrAddString("System"),
metadata.GetOrAddString("Console"));
// Get reference to Console.WriteLine(string) method.
var consoleWriteLineSignature = new BlobBuilder();
new BlobEncoder(consoleWriteLineSignature).
MethodSignature().
Parameters(1,
returnType => returnType.Void(),
parameters => parameters.AddParameter().Type().String());
MemberReferenceHandle consoleWriteLineMemberRef = metadata.AddMemberReference(
systemConsoleTypeRefHandle,
metadata.GetOrAddString("WriteLine"),
metadata.GetOrAddBlob(consoleWriteLineSignature));
// Get reference to Object's constructor.
var parameterlessCtorSignature = new BlobBuilder();
new BlobEncoder(parameterlessCtorSignature).
MethodSignature(isInstanceMethod: true).
Parameters(0, returnType => returnType.Void(), parameters => { });
BlobHandle parameterlessCtorBlobIndex = metadata.GetOrAddBlob(parameterlessCtorSignature);
MemberReferenceHandle objectCtorMemberRef = metadata.AddMemberReference(
systemObjectTypeRef,
metadata.GetOrAddString(".ctor"),
parameterlessCtorBlobIndex);
// Create signature for "void Main()" method.
var mainSignature = new BlobBuilder();
new BlobEncoder(mainSignature).
MethodSignature().
Parameters(0, returnType => returnType.Void(), parameters => { });
var methodBodyStream = new MethodBodyStreamEncoder(ilBuilder);
var codeBuilder = new BlobBuilder();
InstructionEncoder il;
// Emit IL for Program::.ctor
il = new InstructionEncoder(codeBuilder);
// ldarg.0
il.LoadArgument(0);
// call instance void [mscorlib]System.Object::.ctor()
il.Call(objectCtorMemberRef);
// ret
il.OpCode(ILOpCode.Ret);
int ctorBodyOffset = methodBodyStream.AddMethodBody(il);
codeBuilder.Clear();
// Emit IL for Program::Main
var flowBuilder = new ControlFlowBuilder();
il = new InstructionEncoder(codeBuilder, flowBuilder);
// ldstr "hello"
il.LoadString(metadata.GetOrAddUserString("Hello, world"));
// call void [mscorlib]System.Console::WriteLine(string)
il.Call(consoleWriteLineMemberRef);
// ret
il.OpCode(ILOpCode.Ret);
int mainBodyOffset = methodBodyStream.AddMethodBody(il);
codeBuilder.Clear();
// Create method definition for Program::Main
MethodDefinitionHandle mainMethodDef = metadata.AddMethodDefinition(
MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
MethodImplAttributes.IL,
metadata.GetOrAddString("Main"),
metadata.GetOrAddBlob(mainSignature),
mainBodyOffset,
parameterList: default(ParameterHandle));
// Create method definition for Program::.ctor
MethodDefinitionHandle ctorDef = metadata.AddMethodDefinition(
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
MethodImplAttributes.IL,
metadata.GetOrAddString(".ctor"),
parameterlessCtorBlobIndex,
ctorBodyOffset,
parameterList: default(ParameterHandle));
// Create type definition for the special <Module> type that holds global functions
metadata.AddTypeDefinition(
default(TypeAttributes),
default(StringHandle),
metadata.GetOrAddString("<Module>"),
baseType: default(EntityHandle),
fieldList: MetadataTokens.FieldDefinitionHandle(1),
methodList: mainMethodDef);
// Create type definition for ConsoleApplication.Program
metadata.AddTypeDefinition(
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.AutoLayout | TypeAttributes.BeforeFieldInit,
metadata.GetOrAddString("ConsoleApplication"),
metadata.GetOrAddString("Program"),
baseType: systemObjectTypeRef,
fieldList: MetadataTokens.FieldDefinitionHandle(1),
methodList: mainMethodDef);
return mainMethodDef;
}
private static void WritePEImage(
Stream peStream,
MetadataBuilder metadataBuilder,
BlobBuilder ilBuilder,
MethodDefinitionHandle entryPointHandle
)
{
// Create executable with the managed metadata from the specified MetadataBuilder.
var peHeaderBuilder = new PEHeaderBuilder(
imageCharacteristics: Characteristics.ExecutableImage
);
var peBuilder = new ManagedPEBuilder(
peHeaderBuilder,
new MetadataRootBuilder(metadataBuilder),
ilBuilder,
entryPoint: entryPointHandle,
flags: CorFlags.ILOnly,
deterministicIdProvider: content => s_contentId);
// Write executable into the specified stream.
var peBlob = new BlobBuilder();
BlobContentId contentId = peBuilder.Serialize(peBlob);
peBlob.WriteContentTo(peStream);
}
public static void BuildHelloWorldApp()
{
using var peStream = new FileStream(
"ConsoleApplication.exe", FileMode.OpenOrCreate, FileAccess.ReadWrite
);
var ilBuilder = new BlobBuilder();
var metadataBuilder = new MetadataBuilder();
MethodDefinitionHandle entryPoint = EmitHelloWorld(metadataBuilder, ilBuilder);
WritePEImage(peStream, metadataBuilder, ilBuilder, entryPoint);
}
備註
類別 MetadataBuilder 可讓您以程式設計方式產生元件。 這些元件可以儲存至檔案,不同於 類別所產生的 AssemblyBuilder 動態元件,這不支援將元件儲存至 .NET 5+ 和 .NET Core 上的檔案。
MetadataBuilder
API 會操作低階元數據建構,例如數據表或 Blob。 如需使用 C# 動態產生元件的更簡單方式,請參閱 CSharpCompilation Roslyn API。
CLI 元數據的格式是由 ECMA-335 規格所定義。 如需詳細資訊,請參閱 Ecma International 網站上的 標準 ECMA-335 - Common Language Infrastructure (CLI) 。
建構函式
MetadataBuilder(Int32, Int32, Int32, Int32) |
建立用於中繼資料表和堆積的產生器。 |