Author: spouliot
Date: 2008-02-14 12:30:05 -0500 (Thu, 14 Feb 2008)
New Revision: 95666

Added:
   trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CecilRocks.cs
Modified:
   
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CustomAttributeRocks.cs
   trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs
Log:


Added: 
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CecilRocks.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CecilRocks.cs  
2008-02-14 17:27:33 UTC (rev 95665)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CecilRocks.cs  
2008-02-14 17:30:05 UTC (rev 95666)
@@ -0,0 +1,71 @@
+//
+// Gendarme.Framework.Rocks.CecilRocks
+//
+// Authors:
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+using Mono.Cecil;
+
+namespace Gendarme.Framework.Rocks {
+
+       // Here we keep general, Cecil related, rocks
+
+       public static class IMetadataTokenProviderRock {
+
+               /// <summary>
+               /// Return the AssemblyDefinition that correspond to the 
IMetadataTokenProvider
+               /// based object.
+               /// </summary>
+               /// <param name="self">The IMetadataTokenProvider instance 
where the method is applied.</param>
+               /// <returns>The AssemblyDefinition associated with the 
IMetadataTokenProvider or null
+               /// if none can be found</returns>
+               public static AssemblyDefinition GetAssembly (this 
IMetadataTokenProvider self)
+               {
+                       AssemblyDefinition ad = (self as AssemblyDefinition);
+                       if (ad != null)
+                               return ad;
+
+                       TypeDefinition td = (self as TypeDefinition);
+                       if (td != null)
+                               return td.Module.Assembly;
+
+                       MethodDefinition md = (self as MethodDefinition);
+                       if (md != null)
+                               return md.DeclaringType.Module.Assembly;
+
+                       FieldDefinition fd = (self as FieldDefinition);
+                       if (fd != null)
+                               return fd.DeclaringType.Module.Assembly;
+
+                       ParameterDefinition pd = (self as ParameterDefinition);
+                       if (pd != null)
+                               return pd.Method.DeclaringType.Module.Assembly;
+
+                       return null;
+               }
+       }
+}

Modified: 
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CustomAttributeRocks.cs
===================================================================
--- 
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CustomAttributeRocks.cs
        2008-02-14 17:27:33 UTC (rev 95665)
+++ 
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/CustomAttributeRocks.cs
        2008-02-14 17:30:05 UTC (rev 95666)
@@ -77,7 +77,7 @@
                public static bool ContainsAnyType (this 
CustomAttributeCollection self, string[] attributeTypeNames)
                {
                        if (attributeTypeNames == null)
-                               throw new ArgumentNullException 
("attributeNames");
+                               throw new ArgumentNullException 
("attributeTypeNames");
 
                        foreach (CustomAttribute ca in self) {
                                string fullname = 
ca.Constructor.DeclaringType.FullName;

Modified: 
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs   
2008-02-14 17:27:33 UTC (rev 95665)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs   
2008-02-14 17:30:05 UTC (rev 95666)
@@ -31,9 +31,12 @@
 // THE SOFTWARE.
 
 using System;
+using System.Collections.Generic;
 
 using Mono.Cecil;
 
+using Gendarme.Framework.Helpers;
+
 namespace Gendarme.Framework.Rocks {
 
        // add Type[Definition|Reference] extensions methods here
@@ -53,6 +56,21 @@
        public static class TypeRocks {
 
                /// <summary>
+               /// Return an IEnumerable that allows a single loop (like a 
foreach) to
+               /// traverse all MethodDefinition in the type. That includes 
the Constructors
+               /// and Methods collections.
+               /// </summary>
+               /// <param name="self"></param>
+               /// <returns>An IEnumerable to traverse all constructors and 
methods</returns>
+               public static IEnumerable<MethodDefinition> AllMethods (this 
TypeDefinition self)
+               {
+                       foreach (MethodDefinition ctor in self.Constructors)
+                               yield return ctor;
+                       foreach (MethodDefinition method in self.Methods)
+                               yield return method;
+               }
+
+               /// <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>
@@ -63,7 +81,7 @@
                /// </remarks>
                public static MethodDefinition GetMethod (this TypeDefinition 
self, MethodSignature signature)
                {
-                       foreach (MethodDefinition method in self.Methods) {
+                       foreach (MethodDefinition method in self.AllMethods ()) 
{
                                if (signature.Matches (method))
                                        return method;
                        }
@@ -82,7 +100,7 @@
                /// <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) {
+                       foreach (MethodDefinition method in self.AllMethods ()) 
{
                                if (name != null && method.Name != name)
                                        continue;
                                if ((method.Attributes & attributes) != 
attributes)
@@ -276,6 +294,26 @@
                }
 
                /// <summary>
+               /// Check if the type is a delegate.
+               /// </summary>
+               /// <param name="self">The TypeDefinition on which the 
extension method can be called.</param>
+               /// <returns>True if the type is a delegate, False 
otherwise.</returns>
+               public static bool IsDelegate (this TypeDefinition self)
+               {
+                       // e.g. this occurs for <Module>
+                       if (self.BaseType == null)
+                               return false;
+
+                       switch (self.BaseType.FullName) {
+                       case "System.Delegate":
+                       case "System.MulticastDelegate":
+                               return true;
+                       default:
+                               return false;
+                       }
+               }
+
+               /// <summary>
                /// Check if the type is a enumeration flags.
                /// </summary>
                /// <param name="self">The TypeDefinition on which the 
extension method can be called.</param>

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to