Author: spouliot Date: 2008-01-18 19:09:24 -0500 (Fri, 18 Jan 2008) New Revision: 93305
Modified: trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs Log: 2008-01-18 Sebastien Pouliot <[EMAIL PROTECTED]> * TypeRocks.cs: Add [Get|Has]Method rocks from Andreas Noever. Mark GetFinalizer as obsolete (will be removed soon). Modified: trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog =================================================================== --- trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog 2008-01-19 00:06:15 UTC (rev 93304) +++ trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog 2008-01-19 00:09:24 UTC (rev 93305) @@ -1,3 +1,8 @@ +2008-01-18 Sebastien Pouliot <[EMAIL PROTECTED]> + + * TypeRocks.cs: Add [Get|Has]Method rocks from Andreas Noever. Mark + GetFinalizer as obsolete (will be removed soon). + 2008-01-13 Sebastien Pouliot <[EMAIL PROTECTED]> * MethodRocks.cs: Add IsFinalizer [Daniel Abramov] Modified: trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs =================================================================== --- trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs 2008-01-19 00:06:15 UTC (rev 93304) +++ trunk/cecil/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs 2008-01-19 00:09:24 UTC (rev 93305) @@ -6,13 +6,11 @@ // Daniel Abramov <[EMAIL PROTECTED]> // Adrian Tsai <[EMAIL PROTECTED]> // Andreas Noever <[EMAIL PROTECTED]> -// Andreas Noever <[EMAIL PROTECTED]> // // Copyright (C) 2007-2008 Novell, Inc (http://www.novell.com) // (C) 2007 Daniel Abramov // Copyright (c) 2007 Adrian Tsai // (C) 2008 Andreas Noever -// (C) 2008 Andreas Noever // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -59,16 +57,133 @@ /// </summary> /// <param name="self">The TypeDefinition on which the extension method can be called.</param> /// <returns>The MethodDefinition of the finalizer or null if the type has no finalizer.</returns> + [Obsolete ("use the more general GetMethod (MethodSignatures.Finalize)")] public static MethodDefinition GetFinalizer (this TypeDefinition self) { + return self.GetMethod (MethodSignatures.Finalize); + } + + /// <summary> + /// Returns the first MethodDefinition that satisfies a given MethodSignature. + /// </summary> + /// <param name="self">The TypeDefinition 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 TypeDefinition self, MethodSignature signature) + { foreach (MethodDefinition method in self.Methods) { - if (method.IsFinalizer ()) + if (signature.Matches (method)) return method; } return null; } /// <summary> + /// Searches for a method. + /// </summary> + /// <param name="self">The TypeDefinition on which the extension method can be called.</param> + /// <param name="attributes">An attribute mask matched against the attributes of the method.</param> + /// <param name="name">The name of the method to match. Ignored if null.</param> + /// <param name="returnType">The full name (Namespace.Type) of the return type. Ignored if null.</param> + /// <param name="parameters">An array of full names (Namespace.Type) of parameter types. Ignored if null. Null entries act as wildcards.</param> + /// <param name="customCondition">A custom condition that is called for each MethodDefinition that satisfies all other conditions. Ignored if null.</param> + /// <returns>The first MethodDefinition that satisfies all conditions.</returns> + public static MethodDefinition GetMethod (this TypeDefinition self, MethodAttributes attributes, string name, string returnType, string [] parameters, Func<MethodDefinition, bool> customCondition) + { + foreach (MethodDefinition method in self.Methods) { + if (name != null && method.Name != name) + continue; + if ((method.Attributes & attributes) != attributes) + continue; + if (returnType != null && method.ReturnType.ReturnType.FullName != returnType) + continue; + if (parameters != null) { + if (parameters.Length != method.Parameters.Count) + continue; + bool parameterError = false; + for (int i = 0; i < parameters.Length; i++) { + if (parameters [i] == null) + continue;//ignore parameter + if (parameters [i] != method.Parameters [i].ParameterType.FullName) { + parameterError = true; + break; + } + } + if (parameterError) + break; + } + if (customCondition != null && !customCondition (method)) + continue; + return method; + } + return null; + } + + /// <summary> + /// Searches for a method by name, returnType, parameters and attributes. + /// </summary> + /// <param name="self">The TypeDefinition on which the extension method can be called.</param> + /// <param name="attributes">An attribute mask matched against the attributes of the method.</param> + /// <param name="name">The name of the method to match. Ignored if null.</param> + /// <param name="returnType">The full name (Namespace.Type) of the return type. Ignored if null.</param> + /// <param name="parameters">An array of full names (Namespace.Type) of parameter types. Ignored if null. Null entries act as wildcard.</param> + /// <returns>The first MethodDefinition that satisfies all conditions.</returns> + public static MethodDefinition GetMethod (this TypeDefinition self, MethodAttributes attributes, string name, string returnType, string [] parameters) + { + return self.GetMethod (attributes, name, returnType, parameters, null); + } + + /// <summary> + /// Searches for a method by attributes and by name. + /// </summary> + /// <param name="self">The TypeDefinition on which the extension method can be called.</param> + /// <param name="attributes">An attribute mask matched against the attributes of the method.</param> + /// <param name="name">The name of the method to match. Ignored if null.</param> + /// <returns>The first MethodDefinition that satisfies all conditions.</returns> + public static MethodDefinition GetMethod (this TypeDefinition self, MethodAttributes attributes, string name) + { + return self.GetMethod (attributes, name, null, null, null); + } + + /// <summary> + /// Searches for a method by name, returnType and parameters. + /// </summary> + /// <param name="self">The TypeDefinition on which the extension method can be called.</param> + /// <param name="name">The name of the method to match. Ignored if null.</param> + /// <param name="returnType">The full name (Namespace.Type) of the return type. Ignored if null.</param> + /// <param name="parameters">An array of full names (Namespace.Type) of parameter types. Ignored if null. Null entries act as wildcards.</param> + /// <returns>The first MethodDefinition that satisfies all conditions.</returns> + public static MethodDefinition GetMethod (this TypeDefinition self, string name, string returnType, string [] parameters) + { + return self.GetMethod (0, name, returnType, parameters, null); + } + + /// <summary> + /// Searches for a method with a specific name. + /// </summary> + /// <param name="self">The TypeDefinition on which the extension method can be called.</param> + /// <param name="name">The name of the method to match.</param> + /// <returns>The first MethodDefinition with a specifiy name.</returns> + public static MethodDefinition GetMethod (this TypeDefinition self, string name) + { + return self.GetMethod (0, name, null, null, null); + } + + /// <summary> + /// Searches for a method using a custom condition. + /// </summary> + /// <param name="self">The TypeDefinition on which the extension method can be called.</param> + /// <param name="customCondition">A custom condition that is called for each MethodDefinition.</param> + /// <returns>The first MethodDefinition that satisfies the customCondition.</returns> + public static MethodDefinition GetMethod (this TypeDefinition self, Func<MethodDefinition, bool> customCondition) + { + return self.GetMethod (0, null, null, null, customCondition); + } + + /// <summary> /// Check if the type contains an attribute of a specified type. /// </summary> /// <param name="self">The TypeReference on which the extension method can be called.</param> @@ -81,6 +196,17 @@ } /// <summary> + /// Checks if at least one Method satisfies a given MethodSignature. + /// </summary> + /// <param name="self">The TypeDefinition 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 TypeDefinition self, MethodSignature signature) + { + return (self.GetMethod (signature) != null); + } + + /// <summary> /// Check if the type implemented a specified interface. Note that it is possible that /// we might now be able to know all implementations since the assembly where /// the information resides could be unavailable. _______________________________________________ Mono-patches maillist - Mono-patches@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-patches