Exemple #1
0
        public static MethodReference FindBaseMethod(this MethodDefinition methodDefinition)
        {
            TypeReference baseTypeReference = methodDefinition.DeclaringType.BaseType;

            while (baseTypeReference != null)
            {
                TypeDefinition   baseTypeDefinition       = baseTypeReference.Resolve();
                MethodDefinition matchingMethodDefinition =
                    MetadataResolver.GetMethod(baseTypeDefinition.Methods, methodDefinition);
                if (matchingMethodDefinition != null)
                {
                    MethodReference importedReference =
                        methodDefinition.Module.ImportReference(matchingMethodDefinition);
                    if (!baseTypeReference.IsGenericInstance)
                    {
                        return(importedReference);
                    }

                    GenericInstanceType genericInstanceType  = (GenericInstanceType)baseTypeReference;
                    TypeReference[]     genericArgumentTypes = genericInstanceType.GenericArguments
                                                               .Select(reference => methodDefinition.Module.ImportReference(reference))
                                                               .ToArray();
                    return(importedReference.CreateGenericMethod(genericArgumentTypes));
                }

                baseTypeReference = baseTypeDefinition.BaseType;
            }

            return(methodDefinition);
        }
Exemple #2
0
        public static MethodReference CreateGenericMethodIfNeeded(this MethodReference methodReference)
        {
            TypeReference declaringType = methodReference.DeclaringType;

            while (declaringType?.HasGenericParameters == false)
            {
                declaringType = declaringType.Resolve().BaseType;
            }

            return(declaringType?.HasGenericParameters == true
                ? methodReference.CreateGenericMethod(
                       declaringType.GenericParameters.Select(parameter => parameter.GetElementType()).ToArray())
                : methodReference);
        }