本文整理汇总了C#中System.Reflection.Metadata.Ecma335.MetadataBuilder.GetOrAddString方法的典型用法代码示例。如果您正苦于以下问题:C# MetadataBuilder.GetOrAddString方法的具体用法?C# MetadataBuilder.GetOrAddString怎么用?C# MetadataBuilder.GetOrAddString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Metadata.Ecma335.MetadataBuilder
的用法示例。
在下文中一共展示了MetadataBuilder.GetOrAddString方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BasicValidationEmit
private static MethodDefinitionHandle BasicValidationEmit(MetadataBuilder metadata, BlobBuilder ilBuilder)
{
metadata.AddModule(
0,
metadata.GetOrAddString("ConsoleApplication.exe"),
metadata.GetOrAddGuid(Guid.NewGuid()),
default(GuidHandle),
default(GuidHandle));
metadata.AddAssembly(
metadata.GetOrAddString("ConsoleApplication"),
version: new Version(0, 0, 0, 0),
culture: default(StringHandle),
publicKey: default(BlobHandle),
flags: default(AssemblyFlags),
hashAlgorithm: AssemblyHashAlgorithm.Sha1);
var mscorlibAssemblyRef = metadata.AddAssemblyReference(
name: metadata.GetOrAddString("mscorlib"),
version: new Version(4, 0, 0, 0),
culture: default(StringHandle),
publicKeyOrToken: metadata.GetOrAddBlob(ImmutableArray.Create<byte>(0xB7, 0x7A, 0x5C, 0x56, 0x19, 0x34, 0xE0, 0x89)),
flags: default(AssemblyFlags),
hashValue: default(BlobHandle));
var systemObjectTypeRef = metadata.AddTypeReference(
mscorlibAssemblyRef,
metadata.GetOrAddString("System"),
metadata.GetOrAddString("Object"));
var systemConsoleTypeRefHandle = metadata.AddTypeReference(
mscorlibAssemblyRef,
metadata.GetOrAddString("System"),
metadata.GetOrAddString("Console"));
var consoleWriteLineSignature = new BlobBuilder();
new BlobEncoder(consoleWriteLineSignature).
MethodSignature().
Parameters(1,
returnType => returnType.Void(),
parameters => parameters.AddParameter().Type().String());
var consoleWriteLineMemberRef = metadata.AddMemberReference(
systemConsoleTypeRefHandle,
metadata.GetOrAddString("WriteLine"),
metadata.GetOrAddBlob(consoleWriteLineSignature));
var parameterlessCtorSignature = new BlobBuilder();
new BlobEncoder(parameterlessCtorSignature).
MethodSignature(isInstanceMethod: true).
Parameters(0, returnType => returnType.Void(), parameters => { });
var parameterlessCtorBlobIndex = metadata.GetOrAddBlob(parameterlessCtorSignature);
var objectCtorMemberRef = metadata.AddMemberReference(
systemObjectTypeRef,
metadata.GetOrAddString(".ctor"),
parameterlessCtorBlobIndex);
var mainSignature = new BlobBuilder();
new BlobEncoder(mainSignature).
MethodSignature().
Parameters(0, returnType => returnType.Void(), parameters => { });
var methodBodies = new MethodBodiesEncoder(ilBuilder);
var codeBuilder = new BlobBuilder();
var branchBuilder = new BranchBuilder();
InstructionEncoder il;
//
// Program::.ctor
//
int ctorBodyOffset;
il = new InstructionEncoder(codeBuilder);
// ldarg.0
il.LoadArgument(0);
// call instance void [mscorlib]System.Object::.ctor()
il.Call(objectCtorMemberRef);
// ret
il.OpCode(ILOpCode.Ret);
methodBodies.AddMethodBody().WriteInstructions(codeBuilder, out ctorBodyOffset);
codeBuilder.Clear();
//
// Program::Main
//
int mainBodyOffset;
il = new InstructionEncoder(codeBuilder, branchBuilder);
var endLabel = il.DefineLabel();
// .try
int tryOffset = il.Offset;
//.........这里部分代码省略.........
示例2: Strings
public void Strings()
{
var mdBuilder = new MetadataBuilder();
for (int i = 0; i < 0x10000 / 4; i++)
{
mdBuilder.GetOrAddString($"<{i}>");
}
mdBuilder.AddModule(0, default(StringHandle), default(GuidHandle), default(GuidHandle), default(GuidHandle));
var rootBuilder = new MetadataRootBuilder(mdBuilder);
var mdBlob = new BlobBuilder();
rootBuilder.Serialize(mdBlob, 0, 0);
// validate sizes table rows that reference guids:
using (var mdProvider = MetadataReaderProvider.FromMetadataImage(mdBlob.ToImmutableArray()))
{
const int LARGE = 4;
var mdReader = mdProvider.GetMetadataReader();
Assert.Equal(2 + 3 * 2 + LARGE, mdReader.ModuleTable.RowSize);
Assert.Equal(2 + LARGE + LARGE, mdReader.TypeRefTable.RowSize);
Assert.Equal(4 + LARGE + LARGE + 2 + 2 + 2, mdReader.TypeDefTable.RowSize);
Assert.Equal(2 + LARGE + 2, mdReader.FieldTable.RowSize);
Assert.Equal(8 + LARGE + 2 + 2, mdReader.MethodDefTable.RowSize);
Assert.Equal(4 + LARGE, mdReader.ParamTable.RowSize);
Assert.Equal(2 + 2, mdReader.InterfaceImplTable.RowSize);
Assert.Equal(2 + LARGE + 2, mdReader.MemberRefTable.RowSize);
Assert.Equal(2 + 2 + 2, mdReader.ConstantTable.RowSize);
Assert.Equal(2 + 2 + 2, mdReader.CustomAttributeTable.RowSize);
Assert.Equal(2 + 2, mdReader.FieldMarshalTable.RowSize);
Assert.Equal(2 + 2 + 2, mdReader.DeclSecurityTable.RowSize);
Assert.Equal(6 + 2, mdReader.ClassLayoutTable.RowSize);
Assert.Equal(4 + 2, mdReader.FieldLayoutTable.RowSize);
Assert.Equal(2, mdReader.StandAloneSigTable.RowSize);
Assert.Equal(2 + 2, mdReader.EventMapTable.RowSize);
Assert.Equal(2 + LARGE + 2, mdReader.EventTable.RowSize);
Assert.Equal(2 + 2, mdReader.PropertyMapTable.RowSize);
Assert.Equal(2 + LARGE + 2, mdReader.PropertyTable.RowSize);
Assert.Equal(2 + 2 + 2, mdReader.MethodSemanticsTable.RowSize);
Assert.Equal(2 + 2 + 2, mdReader.MethodImplTable.RowSize);
Assert.Equal(LARGE, mdReader.ModuleRefTable.RowSize);
Assert.Equal(2, mdReader.TypeSpecTable.RowSize);
Assert.Equal(2 + 2 + LARGE + 2, mdReader.ImplMapTable.RowSize);
Assert.Equal(4 + 2, mdReader.FieldRvaTable.RowSize);
Assert.Equal(16 + 2 + LARGE + LARGE, mdReader.AssemblyTable.RowSize);
Assert.Equal(12 + 2 + LARGE + LARGE + 2, mdReader.AssemblyRefTable.RowSize);
Assert.Equal(4 + LARGE + 2, mdReader.FileTable.RowSize);
Assert.Equal(8 + LARGE + LARGE + 2, mdReader.ExportedTypeTable.RowSize);
Assert.Equal(8 + LARGE + 2, mdReader.ManifestResourceTable.RowSize);
Assert.Equal(2 + 2, mdReader.NestedClassTable.RowSize);
Assert.Equal(4 + 2 + LARGE, mdReader.GenericParamTable.RowSize);
Assert.Equal(2 + 2, mdReader.MethodSpecTable.RowSize);
Assert.Equal(2 + 2, mdReader.GenericParamConstraintTable.RowSize);
Assert.Equal(2 + 2 + 2 + 2, mdReader.DocumentTable.RowSize);
Assert.Equal(2 + 2, mdReader.MethodDebugInformationTable.RowSize);
Assert.Equal(2 + 2 + 2 + 2 + 4 + 4, mdReader.LocalScopeTable.RowSize);
Assert.Equal(2 + 2 + LARGE, mdReader.LocalVariableTable.RowSize);
Assert.Equal(LARGE + 2, mdReader.LocalConstantTable.RowSize);
Assert.Equal(2 + 2, mdReader.ImportScopeTable.RowSize);
Assert.Equal(2 + 2, mdReader.StateMachineMethodTable.RowSize);
Assert.Equal(2 + 2 + 2, mdReader.CustomDebugInformationTable.RowSize);
}
}
示例3: ComplexEmit
private static MethodDefinitionHandle ComplexEmit(MetadataBuilder metadata, BlobBuilder ilBuilder)
{
metadata.AddModule(
0,
metadata.GetOrAddString("ConsoleApplication.exe"),
metadata.GetOrAddGuid(Guid.NewGuid()),
default(GuidHandle),
default(GuidHandle));
metadata.AddAssembly(
metadata.GetOrAddString("ConsoleApplication"),
version: new Version(0, 0, 0, 0),
culture: default(StringHandle),
publicKey: default(BlobHandle),
flags: default(AssemblyFlags),
hashAlgorithm: AssemblyHashAlgorithm.Sha1);
var mscorlibAssemblyRef = metadata.AddAssemblyReference(
name: metadata.GetOrAddString("mscorlib"),
version: new Version(4, 0, 0, 0),
culture: default(StringHandle),
publicKeyOrToken: metadata.GetOrAddBlob(ImmutableArray.Create<byte>(0xB7, 0x7A, 0x5C, 0x56, 0x19, 0x34, 0xE0, 0x89)),
flags: default(AssemblyFlags),
hashValue: default(BlobHandle));
// TypeRefs:
var systemObjectTypeRef = metadata.AddTypeReference(mscorlibAssemblyRef, metadata.GetOrAddString("System"), metadata.GetOrAddString("Object"));
var dictionaryTypeRef = metadata.AddTypeReference(mscorlibAssemblyRef, metadata.GetOrAddString("System.Collections.Generic"), metadata.GetOrAddString("Dictionary`2"));
var strignBuilderTypeRef = metadata.AddTypeReference(mscorlibAssemblyRef, metadata.GetOrAddString("System.Text"), metadata.GetOrAddString("StringBuilder"));
var typeTypeRef = metadata.AddTypeReference(mscorlibAssemblyRef, metadata.GetOrAddString("System"), metadata.GetOrAddString("Type"));
var int32TypeRef = metadata.AddTypeReference(mscorlibAssemblyRef, metadata.GetOrAddString("System"), metadata.GetOrAddString("Int32"));
var runtimeTypeHandleRef = metadata.AddTypeReference(mscorlibAssemblyRef, metadata.GetOrAddString("System"), metadata.GetOrAddString("RuntimeTypeHandle"));
var invalidOperationExceptionTypeRef = metadata.AddTypeReference(mscorlibAssemblyRef, metadata.GetOrAddString("System"), metadata.GetOrAddString("InvalidOperationException"));
// TypeDefs:
metadata.AddTypeDefinition(
default(TypeAttributes),
default(StringHandle),
metadata.GetOrAddString("<Module>"),
baseType: default(EntityHandle),
fieldList: MetadataTokens.FieldDefinitionHandle(1),
methodList: MetadataTokens.MethodDefinitionHandle(1));
var baseClassTypeDef = metadata.AddTypeDefinition(
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.AutoLayout | TypeAttributes.BeforeFieldInit | TypeAttributes.Abstract,
metadata.GetOrAddString("Lib"),
metadata.GetOrAddString("BaseClass"),
systemObjectTypeRef,
fieldList: MetadataTokens.FieldDefinitionHandle(1),
methodList: MetadataTokens.MethodDefinitionHandle(1));
var derivedClassTypeDef = metadata.AddTypeDefinition(
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.AutoLayout | TypeAttributes.BeforeFieldInit,
metadata.GetOrAddString("Lib"),
metadata.GetOrAddString("DerivedClass"),
baseClassTypeDef,
fieldList: MetadataTokens.FieldDefinitionHandle(4),
methodList: MetadataTokens.MethodDefinitionHandle(1));
// FieldDefs:
// Field1
var baseClassNumberFieldDef = metadata.AddFieldDefinition(
FieldAttributes.Private,
metadata.GetOrAddString("_number"),
metadata.GetOrAddBlob(BuildSignature(e => e.FieldSignature().Int32())));
// Field2
var baseClassNegativeFieldDef = metadata.AddFieldDefinition(
FieldAttributes.Assembly,
metadata.GetOrAddString("negative"),
metadata.GetOrAddBlob(BuildSignature(e => e.FieldSignature().Boolean())));
// Field3
var derivedClassSumCacheFieldDef = metadata.AddFieldDefinition(
FieldAttributes.Assembly,
metadata.GetOrAddString("_sumCache"),
metadata.GetOrAddBlob(BuildSignature(e =>
{
var inst = e.FieldSignature().GenericInstantiation(isValueType: false, typeRefDefSpec: dictionaryTypeRef, genericArgumentCount: 2);
inst.AddArgument().Int32();
inst.AddArgument().Object();
})));
// Field4
var derivedClassCountFieldDef = metadata.AddFieldDefinition(
FieldAttributes.Assembly,
metadata.GetOrAddString("_count"),
metadata.GetOrAddBlob(BuildSignature(e => e.FieldSignature().SZArray().Int32())));
// Field5
var derivedClassBCFieldDef = metadata.AddFieldDefinition(
FieldAttributes.Assembly,
metadata.GetOrAddString("_bc"),
metadata.GetOrAddBlob(BuildSignature(e => e.FieldSignature().TypeDefOrRefOrSpec(isValueType: false, typeRefDefSpec: baseClassTypeDef))));
var methodBodies = new MethodBodiesEncoder(ilBuilder);
//.........这里部分代码省略.........