當前位置: 首頁>>代碼示例>>C#>>正文


C# MethodReference.MakeGenericInstanceMethod方法代碼示例

本文整理匯總了C#中Mono.Cecil.MethodReference.MakeGenericInstanceMethod方法的典型用法代碼示例。如果您正苦於以下問題:C# MethodReference.MakeGenericInstanceMethod方法的具體用法?C# MethodReference.MakeGenericInstanceMethod怎麽用?C# MethodReference.MakeGenericInstanceMethod使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Mono.Cecil.MethodReference的用法示例。


在下文中一共展示了MethodReference.MakeGenericInstanceMethod方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ExposeMethod

	private MethodDefinition ExposeMethod(TypeDefinition typeToProcess, FieldReference fieldToProcess, MethodReference methodToExpose, ExposeMode exposeMode, MethodSemanticsAttributes semanticAttributes)
	{
		var typeToExpose = fieldToProcess.FieldType;

		var genericTypeToExpose = typeToExpose as GenericInstanceType;
		MethodReference methodToExposeWithResolvedGenerics = null;

		if (genericTypeToExpose != null)
		{
			methodToExposeWithResolvedGenerics = methodToExpose.With(declaringType: methodToExpose.DeclaringType.MakeGenericInstanceType(genericTypeToExpose.GenericArguments.ToArray()), resolveGenericReturnTypeAndParameterTypes: true);
			methodToExpose = methodToExpose.With(declaringType: methodToExpose.DeclaringType.MakeGenericInstanceType(genericTypeToExpose.GenericArguments.ToArray()), resolveGenericReturnTypeAndParameterTypes: false);
		}
		var name = exposeMode == ExposeMode.ImplementExplicit ? typeToExpose.FullName + "." + methodToExpose.Name : methodToExpose.Name;

		MethodAttributes methodAttributes = (exposeMode == ExposeMode.ImplementExplicit ? MethodAttributes.Private : MethodAttributes.Public);
		if (exposeMode == ExposeMode.ImplementImplicit || exposeMode == ExposeMode.ImplementExplicit)
			methodAttributes |= MethodAttributes.Final;
		methodAttributes |= MethodAttributes.HideBySig;
		if (semanticAttributes != MethodSemanticsAttributes.None)
			methodAttributes |= MethodAttributes.SpecialName;
		if (exposeMode == ExposeMode.ImplementImplicit || exposeMode == ExposeMode.ImplementExplicit)
			methodAttributes |= MethodAttributes.NewSlot | MethodAttributes.Virtual;

		var method = new MethodDefinition(name, methodAttributes, (methodToExposeWithResolvedGenerics ?? methodToExpose).ReturnType);
		foreach (var genericParameter in (methodToExposeWithResolvedGenerics ?? methodToExpose).GenericParameters)
		{
			method.GenericParameters.Add(new GenericParameter(genericParameter.Name, method));
		}
		foreach (var parameter in (methodToExposeWithResolvedGenerics ?? methodToExpose).Parameters)
		{
			method.Parameters.Add(new ParameterDefinition(parameter.Name, parameter.Attributes, parameter.ParameterType));
		}

		var existingMethod = typeToProcess.GetMethodLike(method);
		if (existingMethod != null)
			return existingMethod;

		bool methodReturnsVoid = method.ReturnType.FullName == ModuleDefinition.TypeSystem.Void.FullName;

		if (!methodReturnsVoid)
		{
			method.Body.Variables.Add(new VariableDefinition(method.ReturnType));
			method.Body.InitLocals = true;
		}

		var instructions = method.Body.Instructions;
		instructions.Add(Instruction.Create(OpCodes.Nop));
		instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // Load this
		instructions.Add(Instruction.Create(OpCodes.Ldfld,
			fieldToProcess.DeclaringType.HasGenericParameters ?
			fieldToProcess.With(declaringType: fieldToProcess.DeclaringType.MakeGenericInstanceTypeWithGenericParametersAsGenericArguments())
			: fieldToProcess)
		);

		if (methodToExpose.Parameters.Count >= 1) instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
		if (methodToExpose.Parameters.Count >= 2) instructions.Add(Instruction.Create(OpCodes.Ldarg_2));
		if (methodToExpose.Parameters.Count >= 3) instructions.Add(Instruction.Create(OpCodes.Ldarg_3));
		if (methodToExpose.Parameters.Count >= 4)
		{
			for (var i = 3; i < methodToExpose.Parameters.Count; i++)
			{
				instructions.Add(Instruction.Create(OpCodes.Ldarg_S, i + 1));
			}
		}
		instructions.Add(Instruction.Create(OpCodes.Callvirt, methodToExpose.HasGenericParameters ? methodToExpose.MakeGenericInstanceMethod(method.GenericParameters.ToArray()) : methodToExpose));
		if (methodReturnsVoid)
			instructions.Add(Instruction.Create(OpCodes.Nop));
		else
		{
			instructions.Add(Instruction.Create(OpCodes.Stloc_0));
			var inst = Instruction.Create(OpCodes.Ldloc_0);
			instructions.Add(Instruction.Create(OpCodes.Br_S, inst));
			instructions.Add(inst);
		}
		instructions.Add(Instruction.Create(OpCodes.Ret));

		if (exposeMode == ExposeMode.ImplementExplicit)
			method.Overrides.Add(methodToExpose);

		AddFodyGeneratedAttributes(method);

		typeToProcess.Methods.Add(method);
		return method;
	}
開發者ID:kedarvaidya,項目名稱:Expose.Fody,代碼行數:84,代碼來源:ModuleWeaver.cs


注:本文中的Mono.Cecil.MethodReference.MakeGenericInstanceMethod方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。