private bool IsRule (TypeReference type) { var typeName = type.FullName; bool result; if (!typeIsRule.TryGetValue (typeName, out result)) { result = type.Implements ("Gendarme.Framework.IRule"); typeIsRule [typeName] = result; } return result; }
static bool IsICollection (TypeReference type) { if (type.Implements ("System.Collections", "ICollection")) return true; return type.Implements ("System.Collections.Generic", "ICollection`1"); }
static string InheritsOrImplements(TypeReference type, string nameSpace, string name) { if (type.Inherits (nameSpace, name) || type.Implements (nameSpace, name)) return String.Empty; return String.Format (CultureInfo.InvariantCulture, "'{0}' should only be used for types that inherits or implements '{1}.{2}'.", type.Name, nameSpace, name); }
static string CheckDictionary(TypeReference type) { if (type.Implements ("System.Collections", "IDictionary") || type.Implements ("System.Collections.Generic", "IDictionary`2")) return String.Empty; return "'Dictionary' should only be used for types implementing IDictionary and IDictionary<TKey,TValue>."; }
static string CheckCollection(TypeReference type) { if (type.Implements ("System.Collections", "ICollection") || type.Implements ("System.Collections", "IEnumerable") || type.Implements ("System.Collections.Generic", "ICollection`1")) return String.Empty; if (type.Inherits ("System.Collections", "Queue") || type.Inherits ("System.Collections", "Stack") || type.Inherits ("System.Data", "DataSet") || type.Inherits ("System.Data", "DataTable")) return String.Empty; return "'Collection' should only be used for implementing ICollection or IEnumerable or inheriting from Queue, Stack, DataSet and DataTable."; }
static string InheritsOrImplements (TypeReference type, string subtype) { if (type.Inherits (subtype) || type.Implements (subtype)) return String.Empty; return String.Format ("'{0}' should only be used for types that inherits or implements {1}.", type.Name, subtype); }
public override RuleResult CheckMethod (MethodDefinition method) { if (!method.HasBody) return RuleResult.DoesNotApply; //the rule does not apply to the particular case of ToString() //that have its own ToStringShouldNotReturnNullRule if (!method.HasParameters && method.Name == "ToString") return RuleResult.DoesNotApply; //only apply to methods returning string, array, or IEnumerable-impl return_type = method.ReturnType; string_return_type = (return_type.FullName == "System.String"); array_return_type = return_type.IsArray; ienumerable_return_type = return_type.Implements ("System.Collections.IEnumerable"); if (!string_return_type && !array_return_type && !ienumerable_return_type) return RuleResult.DoesNotApply; return base.CheckMethod (method); }