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);
        }
		private static bool DoesAllSignaturesMatchType (TypeReference type, IEnumerable<MethodSignature> signatures)
		{
			bool match = true;
			foreach (MethodSignature signature in signatures) {
				match &= (null != type.GetMethod (signature));
			}
			return match;
		}
		private bool CheckDelegate (TypeReference type)
		{
			MethodReference invoke = type.GetMethod (MethodSignatures.Invoke);
			if (invoke == null)
				return false;

			// we cannot short-circuit since we would miss reporting some defects
			bool valid = CheckReturnVoid (type, invoke);
			valid &= CheckAmountOfParameters (type, invoke);
			valid &= CheckParameterTypes (type, invoke);

			IList<ParameterDefinition> pdc = invoke.Parameters;
			if (pdc.Count > 0) {
				valid &= CheckParameterName (type, pdc [0], "sender");
				if (pdc.Count > 1)
					valid &= CheckParameterName (type, pdc [1], "e");
			}
			return valid;
		}
		private void GetMethods (TypeReference type)	
		{
			string full_name = type.GetFullName ();
			args1 [0] = full_name;
			AddMethod (type.GetMethod (MethodSignatures.Equals));
			AddMethod (type.GetMethod ("Equals", "System.Boolean", args1));

			AddMethod (type.GetMethod ("CompareTo", "System.Int32", args1));	// generic version
			AddMethod (type.GetMethod (CompareTo));								// non-generic version

			// Note that we don't want to use MethodSignatures for these 
			// because we don't want any weird overloads.
			args2 [0] = full_name;
			args2 [1] = full_name;
			AddMethod (type.GetMethod ("op_Equality", "System.Boolean", args2));	
			AddMethod (type.GetMethod ("op_Inequality", "System.Boolean", args2));

			AddMethod (type.GetMethod ("op_LessThan", "System.Boolean", args2));
			AddMethod (type.GetMethod ("op_LessThanOrEqual", "System.Boolean", args2));
			AddMethod (type.GetMethod ("op_GreaterThan", "System.Boolean", args2));
			AddMethod (type.GetMethod ("op_GreaterThanOrEqual", "System.Boolean", args2));
			
			clone.Method = type.GetMethod (MethodSignatures.Clone);
			hash.Method = type.GetMethod (MethodSignatures.GetHashCode);
		}
		private RuleResult CheckDelegate (TypeReference type)
		{
			MethodDefinition method = type.GetMethod ("Invoke");
			// MulticastDelegate inherits from Delegate without overriding Invoke
			if ((method != null) && HasMoreParametersThanAllowed (method))
				Runner.Report (type, Severity.Medium, Confidence.Normal, "This delegate contains a long parameter list.");
			return Runner.CurrentRuleResult;
		}
Exemple #6
0
 public static MethodReference?GetGetMethod(this TypeReference type, string property) =>
 type.GetMethod("get_" + property);
		private void CheckOperatorPair (KeyValuePair<MethodSignature, MethodSignature> pair, 
			TypeReference type, Severity severity)
		{
			MethodDefinition op = type.GetMethod (pair.Key);
			if (op == null) { //first one not defined
				pair = new KeyValuePair<MethodSignature, MethodSignature> (pair.Value, pair.Key); //reverse
				op = type.GetMethod (pair.Key);
				if (op == null)
					return; //neither one is defined
			} else {
				if (type.HasMethod (pair.Value))
					return; //both are defined
			}

			string s = string.Format (CultureInfo.InvariantCulture, Message, pair.Key.Name, pair.Value.Name);
			Runner.Report (op, severity, Confidence.Total, s);
		}
		private static MethodDefinition GetNonAbstractMethod (TypeReference type, MethodSignature signature)
		{
			MethodDefinition method = type.GetMethod (signature);
			if ((method == null) || method.IsAbstract)
				return null;
			return method;
		}