本文整理汇总了C#中Mono.Cecil.TypeDefinition.MakeGenericType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition.MakeGenericType方法的具体用法?C# TypeDefinition.MakeGenericType怎么用?C# TypeDefinition.MakeGenericType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.TypeDefinition
的用法示例。
在下文中一共展示了TypeDefinition.MakeGenericType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessClosure
private ClosureInfo ProcessClosure(AssemblyProcessorContext context, TypeDefinition closureType, TypeReference[] genericParameters)
{
ClosureInfo closure;
if (closures.TryGetValue(closureType, out closure))
return closure;
var closureTypeConstructor = closureType.Methods.FirstOrDefault(x => x.Name == ".ctor");
var closureGenericType = closureType.MakeGenericType(genericParameters);
// Create factory method for pool items
var factoryMethod = new MethodDefinition("<Factory>", MethodAttributes.HideBySig | MethodAttributes.Private | MethodAttributes.Static, closureGenericType);
closureType.Methods.Add(factoryMethod);
factoryMethod.Body.InitLocals = true;
factoryMethod.Body.Variables.Add(new VariableDefinition(closureGenericType));
var factoryMethodProcessor = factoryMethod.Body.GetILProcessor();
// Create and store closure
factoryMethodProcessor.Emit(OpCodes.Newobj, closureTypeConstructor.MakeGeneric(genericParameters));
factoryMethodProcessor.Emit(OpCodes.Stloc_0);
//// Return closure
factoryMethodProcessor.Emit(OpCodes.Ldloc_0);
factoryMethodProcessor.Emit(OpCodes.Ret);
// Create pool field
var poolField = new FieldDefinition("<pool>", FieldAttributes.Public | FieldAttributes.Static, poolType.MakeGenericType(closureGenericType));
closureType.Fields.Add(poolField);
var poolFieldReference = poolField.MakeGeneric(genericParameters);
// Initialize pool
var cctor = GetOrCreateClassConstructor(closureType);
var ilProcessor2 = cctor.Body.GetILProcessor();
var retInstruction = cctor.Body.Instructions.FirstOrDefault(x => x.OpCode == OpCodes.Ret);
ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Ldnull));
ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Ldftn, factoryMethod.MakeGeneric(genericParameters)));
ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Newobj, funcConstructor.MakeGeneric(closureGenericType)));
ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Newobj, poolConstructor.MakeGeneric(closureGenericType)));
ilProcessor2.InsertBefore(retInstruction, ilProcessor2.Create(OpCodes.Stsfld, poolFieldReference));
// Implement IPooledClosure
closureType.Interfaces.Add(pooledClosureType);
// Create reference count field
var countField = new FieldDefinition("<referenceCount>", FieldAttributes.Public, context.Assembly.MainModule.TypeSystem.Int32);
closureType.Fields.Add(countField);
var oountFieldReference = countField.MakeGeneric(genericParameters);
// Create AddReference method
var addReferenceMethod = new MethodDefinition("AddReference", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot, context.Assembly.MainModule.TypeSystem.Void);
var ilProcessor4 = addReferenceMethod.Body.GetILProcessor();
ilProcessor4.Emit(OpCodes.Ldarg_0);
ilProcessor4.Emit(OpCodes.Ldflda, oountFieldReference);
ilProcessor4.Emit(OpCodes.Call, interlockedIncrementMethod);
ilProcessor4.Emit(OpCodes.Pop);
ilProcessor4.Emit(OpCodes.Ret);
closureType.Methods.Add(addReferenceMethod);
// Create Release method
var releaseMethod = new MethodDefinition("Release", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot, context.Assembly.MainModule.TypeSystem.Void);
ilProcessor4 = releaseMethod.Body.GetILProcessor();
retInstruction = ilProcessor4.Create(OpCodes.Ret);
// Check decremented reference count
ilProcessor4.Emit(OpCodes.Ldarg_0);
ilProcessor4.Emit(OpCodes.Ldflda, oountFieldReference);
ilProcessor4.Emit(OpCodes.Call, interlockedDecrementMethod);
ilProcessor4.Emit(OpCodes.Ldc_I4_0);
ilProcessor4.Emit(OpCodes.Ceq);
ilProcessor4.Emit(OpCodes.Brfalse_S, retInstruction);
// Release this to pool
ilProcessor4.Emit(OpCodes.Ldsfld, poolFieldReference);
ilProcessor4.Emit(OpCodes.Ldarg_0);
ilProcessor4.Emit(OpCodes.Callvirt, poolReleaseMethod.MakeGeneric(closureGenericType));
ilProcessor4.Append(retInstruction);
closureType.Methods.Add(releaseMethod);
closures.Add(closureType, closure = new ClosureInfo
{
FactoryMethod = factoryMethod,
AddReferenceMethod = addReferenceMethod,
ReleaseMethod = releaseMethod,
PoolField = poolField
});
return closure;
}