本文整理汇总了C#中TypeDefinition.MakeGenericInstanceType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition.MakeGenericInstanceType方法的具体用法?C# TypeDefinition.MakeGenericInstanceType怎么用?C# TypeDefinition.MakeGenericInstanceType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TypeDefinition
的用法示例。
在下文中一共展示了TypeDefinition.MakeGenericInstanceType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateProxy
private static TypeDefinition CreateProxy(ModuleDefinition moduleDefinition, TypeDefinition type)
{
var proxyType = new TypeDefinition(
null,
string.Format("<{0}>Proxy", type.Name.Split('`')[0]),
TypeAttributes.NestedPrivate | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
moduleDefinition.TypeSystem.Object);
TypeReference proxyTypeRef = proxyType;
type.NestedTypes.Add(proxyType);
proxyType.CustomAttributes.Add(new CustomAttribute(ReferenceFinder.CompilerGeneratedAttributeCtor));
if (type.HasGenericParameters)
{
foreach (var genericParameter in type.GenericParameters)
{
proxyType.GenericParameters.Add(new GenericParameter(genericParameter.Name, proxyType));
}
proxyTypeRef = proxyType.MakeGenericInstanceType(type.GenericParameters.Select(gp => new GenericParameter(gp.Position, gp.Type, gp.Module)).ToArray());
}
var originalType = moduleDefinition.Import(type);
if (type.HasGenericParameters)
originalType = originalType.MakeGenericInstanceType(proxyType.GenericParameters.ToArray());
var field = new FieldDefinition("original", FieldAttributes.Private | FieldAttributes.InitOnly, originalType);
proxyType.Fields.Add(field);
var fieldRef = new FieldReference(field.Name, field.FieldType, proxyTypeRef);
var method = new MethodDefinition(
".ctor",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
moduleDefinition.TypeSystem.Void);
method.Parameters.Add(new ParameterDefinition("original", ParameterAttributes.None, originalType));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, moduleDefinition.Import(moduleDefinition.TypeSystem.Object.Resolve().Constructors().First())));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Stfld, fieldRef));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
proxyType.Methods.Add(method);
return proxyType;
}