public static MethodReference GetBaseMethodInstance(this MethodDefinition target) { MethodDefinition baseMethodDef = null; var baseType = target.DeclaringType.BaseType; while (baseType != null) { if (baseType.MetadataToken.TokenType == TokenType.TypeRef) { break; } var baseTypeDef = baseType.Resolve(); baseMethodDef = MetadataResolver.GetMethod(baseTypeDef.Methods, target); if (baseMethodDef != null && !baseMethodDef.IsAbstract) { if (baseType.IsGenericInstance) { MethodReference baseMethodRef = baseType.Module.Import(baseMethodDef); var baseTypeInstance = (GenericInstanceType)baseType; return(baseMethodRef.MakeGenericMethod(baseTypeInstance.GenericArguments.ToArray())); } break; } else { baseMethodDef = null; } baseType = baseTypeDef.BaseType; } return(baseMethodDef); }
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); }
public static MethodReference GetBaseMethodInstance(this MethodDefinition target) { MethodDefinition baseMethodDef = null; var baseType = target.DeclaringType.BaseType; Collection <TypeReference> availableArgs = null; while (baseType != null) { if (baseType.MetadataToken.TokenType == TokenType.TypeRef) { break; } GenericInstanceType baseTypeInstance = null; if (baseType.IsGenericInstance) { baseTypeInstance = (GenericInstanceType)baseType; bool bGenericArgumentsAvailable = !baseTypeInstance .GenericArguments .Any(arg => arg.MetadataType == MetadataType.Var); if (bGenericArgumentsAvailable) { availableArgs = baseTypeInstance.GenericArguments; } } var baseTypeDef = baseType.Resolve(); baseMethodDef = MetadataResolver.GetMethod(baseTypeDef.Methods, target); if (baseMethodDef != null && !baseMethodDef.IsAbstract) { if (baseType.IsGenericInstance) { MethodReference baseMethodRef = baseType.Module.Import(baseMethodDef); return(baseMethodRef.MakeGenericMethod(availableArgs.ToArray())); } break; } else { baseMethodDef = null; } baseType = baseTypeDef.BaseType; } return(baseMethodDef); }
static bool IsHidingMethod(MethodDefinition method) { var typeDefinition = method.DeclaringType; // If we're an interface, just try and find any method with the same signature // in any of the interfaces that we implement if (typeDefinition.IsInterface) { var interfaceMethods = from @interfaceReference in typeDefinition.Interfaces let interfaceDefinition = @interfaceReference.Resolve() where interfaceDefinition != null select interfaceDefinition.Methods; return(interfaceMethods.Any(ms => MetadataResolver.GetMethod(ms, method) != null)); } // If we're not an interface, find a base method that isn't virtual return(!method.IsVirtual && GetBaseTypes(typeDefinition).Any(d => MetadataResolver.GetMethod(d.Methods, method) != null)); }
public static MethodDefinition GetBaseMethod(MethodDefinition methodDefinition) { if (methodDefinition == null) { throw new ArgumentNullException("methodDefinition"); } if (!methodDefinition.IsVirtual) { return(null); } if (methodDefinition.IsNewSlot) { return(null); } try { var baseType = ResolveBaseType(methodDefinition.DeclaringType); while (baseType != null) { var baseMethod = MetadataResolver.GetMethod(baseType.Methods, methodDefinition); if (baseMethod != null) { return(baseMethod); } baseType = ResolveBaseType(baseType); } } catch { } return(null); }
static MethodDefinition GetMatchingMethod(TypeDefinition type, MethodDefinition method) { return(MetadataResolver.GetMethod(type.Methods, method)); }
private void CheckTypeDefinitions(string assemblyFullName, ModuleDefinition module, List <MemberReference> references) { var types = module.GetTypes(); foreach (var typeDef in types) { if (!typeDef.IsClass) { continue; } if (typeDef.HasInterfaces) { foreach (var interfaceImplementation in typeDef.Interfaces) { var interfaceTypeRef = interfaceImplementation.InterfaceType; references.Add(interfaceTypeRef); try { var interfaceTypeDef = interfaceTypeRef.Resolve(); if (interfaceTypeDef != null) { if (interfaceTypeDef.HasMethods) { foreach (var interfaceMethod in interfaceTypeDef.Methods) { if (interfaceMethod.HasGenericParameters || interfaceMethod.ContainsGenericParameter) { // it's non-trivial to match when generics are involved continue; } bool sawGenerics = false; var matching = FindInterfaceMethodImplementation(typeDef, interfaceMethod, ref sawGenerics); if (matching == null && !sawGenerics) { var interfaceAssembly = GetAssemblyName(interfaceMethod); diagnostics.Add($"In assembly '{assemblyFullName}': Type {typeDef.FullName} does not implement interface method {interfaceMethod.FullName} from assembly {interfaceAssembly}"); } } } } } catch { } } } if (typeDef.HasMethods) { foreach (var methodDef in typeDef.Methods) { try { if (methodDef.HasOverrides) { foreach (var methodOverride in methodDef.Overrides) { references.Add(methodOverride); } } var baseMethod = GetBaseMethod(methodDef); if (baseMethod != null) { var same = MetadataResolver.GetMethod(baseMethod.DeclaringType.Methods, methodDef); if (same == null) { diagnostics.Add($"In assembly '{assemblyFullName}': Failed to find base method for '{methodDef.FullName}'"); } } } catch { } } } } }
public void CollectFrom([NotNull] MethodDefinition definition, [NotNull] HashSet <int> annotated) { HashSet <MetadataToken> tokens; if (!myNotNullAttributeTypeTokens.TryGetValue(definition.Module, out tokens)) { return; } if (definition.HasParameters) { foreach (var parameter in definition.Parameters) { if (IsNotNullAnnotated(parameter, tokens)) { annotated.Add(parameter.Index); } } } if (!definition.IsConstructor) { if (IsNotNullAnnotated(definition, tokens) || IsNotNullAnnotated(definition.MethodReturnType, tokens)) { annotated.Add(-1); // return value } } var declaringType = definition.DeclaringType; if (declaringType == null) { return; // may be global method, yeah } if (definition.IsVirtual) { if (!definition.IsNewSlot) // do not look for overriden methods { var baseReference = declaringType.BaseType; if (baseReference != null) { var baseDefinition = baseReference.Resolve(); if (baseDefinition != null) { var baseMethod = MetadataResolver.GetMethod(baseDefinition.Methods, definition); if (baseMethod != null) { CollectFrom(baseMethod, annotated); } } } } if (definition.HasOverrides) { foreach (var overridenReference in definition.Overrides) { var overridenDefinition = overridenReference.Resolve(); if (overridenDefinition == null) { continue; } CollectFrom(overridenDefinition, annotated); } } else // implicit interface implementations { foreach (var interfaceReference in declaringType.Interfaces) { var interfaceDefinition = interfaceReference.Resolve(); if (interfaceDefinition == null) { continue; } var interfaceMethod = MetadataResolver.GetMethod(interfaceDefinition.Methods, definition); if (interfaceMethod == null) { continue; } CollectFrom(interfaceMethod, annotated); } } } // todo: test inherited props // inherit annotation from property metadata if (definition.IsGetter) { foreach (var propertyDefinition in declaringType.Properties) { if (propertyDefinition.GetMethod != definition) { continue; } if (IsNotNullAnnotated(propertyDefinition, tokens)) { annotated.Add(-1); // return value } break; } } else if (definition.IsSetter) { foreach (var propertyDefinition in declaringType.Properties) { if (propertyDefinition.SetMethod != definition) { continue; } if (IsNotNullAnnotated(propertyDefinition, tokens) && definition.Parameters.Count == 1) { annotated.Add(definition.Parameters[0].Index); } break; } } }