private void CheckCallingBaseMethod (TypeDefinition type, MethodSignature methodSignature) { MethodDefinition method = type.GetMethod (methodSignature); if (method == null) return; // Perhaps should report that doesn't exist the method (only with ctor). foreach (Instruction instruction in method.Body.Instructions) { if (instruction.OpCode.FlowControl == FlowControl.Call) { MethodReference operand = (MethodReference) instruction.Operand; if (methodSignature.Matches (operand) && type.Inherits (operand.DeclaringType.ToString ())) return; } } Runner.Report (method, Severity.High, Confidence.High, String.Format ("The method {0} isn't calling its base method.", method)); }
private void CheckCallingBaseMethod(TypeReference type, MethodSignature methodSignature) { MethodDefinition method = type.GetMethod (methodSignature); if (method == null) return; // Perhaps should report that doesn't exist the method (only with ctor). // is there any Call or Callvirt instructions in the method if (OpCodeBitmask.Calls.Intersect (OpCodeEngine.GetBitmask (method))) { // in this case we check further foreach (Instruction instruction in method.Body.Instructions) { if (instruction.OpCode.FlowControl != FlowControl.Call) continue; MethodReference operand = (MethodReference) instruction.Operand; TypeReference tr = operand.DeclaringType; if (methodSignature.Matches (operand) && type.Inherits (tr.Namespace, tr.Name)) return; } } Runner.Report (method, Severity.High, Confidence.High); }
static bool IsSignatureDictatedByInterface (IMemberDefinition method, MethodSignature sig) { foreach (TypeReference intf_ref in method.DeclaringType.Interfaces) { TypeDefinition intr = intf_ref.Resolve (); if (intr == null) continue; foreach (MethodDefinition md in intr.Methods) { if (sig.Matches (md)) return true; } } return false; }
static bool IsSignatureDictatedByOverride (IMemberDefinition method, MethodSignature sig) { TypeDefinition baseType = method.DeclaringType.BaseType.Resolve (); while (baseType != null) { if (baseType.HasMethods) { foreach (MethodDefinition md in baseType.Methods) { if (!md.IsConstructor && sig.Matches (md)) return true; } } TypeReference tr = baseType.BaseType; baseType = tr == null ? null : tr.Resolve (); } return false; }
/// <summary> /// Returns the first MethodDefinition that satisfies a given MethodSignature. /// </summary> /// <param name="self">The TypeReference on which the extension method can be called.</param> /// <param name="signature">The MethodSignature to match.</param> /// <returns>The first MethodDefinition for wich signature.Matches returns true.</returns> /// <remarks> /// Do not allocate a MethodSignature for only one call. Use one of the other GetMethod overloads instead. /// </remarks> public static MethodDefinition GetMethod (this TypeReference self, MethodSignature signature) { if (signature == null) throw new ArgumentNullException ("signature"); if (self == null) return null; TypeDefinition type = self.Resolve (); if (type == null) return null; if (type.HasMethods) { foreach (MethodDefinition method in type.Methods) { if (signature.Matches (method)) return method; } } return null; }
public void AlwaysTrueCustomLogic_CheckReturnValue () { MethodSignature ms = new MethodSignature (null, "System.Void", (method) => (true)); Assert.IsFalse (ms.Matches (null), "null"); Assert.IsTrue (ms.Matches (GetMethod ("TestMatch")), "any name returning void"); Assert.IsFalse (ms.Matches (GetMethod ("Method")), "bool"); }
public void AlwaysTrueCustomLogic () { MethodSignature ms = new MethodSignature (null, (method) => (true)); Assert.IsFalse (ms.Matches (null), "null"); Assert.IsTrue (ms.Matches (GetMethod ("TestMatch")), "anything"); }