public void TestDefaultConstructor ()
		{
			MethodSignature sig = new MethodSignature ();
			Assert.IsNull (sig.Name, "Name");
			Assert.IsNull (sig.Parameters, "Parameters");
			Assert.IsNull (sig.ReturnType, "ReturnType");
			Assert.AreEqual (String.Empty, sig.ToString (), "ToString");
		}
		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;
		}
Beispiel #6
0
		/// <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;
		}
Beispiel #7
0
		/// <summary>
		/// Checks if at least one Method 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>True if at least one method matches the signature. Otherwise false.</returns>
		public static bool HasMethod (this TypeReference self, MethodSignature signature)
		{
			return ((self != null) && self.GetMethod (signature) != null);
		}
        private bool HasMethod(TypeReference type, MethodSignature method)
        {
            if (type.HasMethod (method))
                return true;

            TypeDefinition td = type.Resolve ();
            return td != null && td.BaseType != null && HasMethod (td.BaseType, method);
        }
		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");
		}
		private static MethodDefinition GetNonAbstractMethod (TypeReference type, MethodSignature signature)
		{
			MethodDefinition method = type.GetMethod (signature);
			if ((method == null) || method.IsAbstract)
				return null;
			return method;
		}