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 上的文件。
API MetadataBuilder
操作低级别的元数据构造,例如表或 blob。 有关使用 C# 动态生成程序集的更简单方法,请参阅 CSharpCompilation Roslyn API。
CLI 元数据的格式由 ECMA-335 规范定义。 有关详细信息,请参阅 Ecma 国际网站上的标准 ECMA-335 - 公共语言基础结构 (CLI) 。
构造函数
MetadataBuilder(Int32, Int32, Int32, Int32) |
创建元数据表和堆的生成器。 |