2002-08-21 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mcs / class / corlib / System / Attribute.cs
index f5bf41446871435f3dabe9c7703bde8dd10959fe..cdad17adbdac63b8b412ed2a4afcc9f7ab42ff60 100644 (file)
-//
-// System.Attribute.cs
-//
-// Author:
-//   Miguel de Icaza (miguel@ximian.com)
-//
-// (C) Ximian, Inc.  http://www.ximian.com
-//
-
-namespace System {
-
-       public abstract class Attribute {
-
-               protected Attribute ()
-               {
-               }
-
-               public virtual object TypeId {
-                       get {
-                               // TODO: Implement me
-                               return null;
-                       }
-               }
-       }
-}
+//\r
+// System.Attribute.cs\r
+//\r
+// Authors:\r
+//   Miguel de Icaza (miguel@ximian.com) - Original\r
+//   Nick D. Drochak II (ndrochak@gol.com) - Implemented most of the guts\r
+//\r
+// (C) Ximian, Inc.  http://www.ximian.com\r
+//\r
+\r
+using System;\r
+using System.Globalization;\r
+\r
+namespace System {\r
+\r
+       [AttributeUsage(AttributeTargets.All)]\r
+       [Serializable]\r
+       public abstract class Attribute {\r
+\r
+               protected Attribute ()\r
+               {\r
+               }\r
+\r
+               public virtual object TypeId {\r
+                       get {\r
+                               // Derived classes should override this default behaviour as appropriate\r
+                               return this.GetType ();\r
+                       }\r
+               }\r
+\r
+               private static void CheckParameters (object element, Type attribute_type)\r
+               {\r
+                       // neither parameter is allowed to be null\r
+                       if (null == element) \r
+                       {\r
+                               throw new ArgumentNullException ("element");\r
+                       }\r
+\r
+                       if (null == attribute_type) \r
+                       {\r
+                               throw new ArgumentNullException ("attribute_type");\r
+                       }\r
+               }\r
+\r
+               private static System.Attribute FindAttribute (object[] attributes)\r
+               {\r
+                       // if there exists more than one attribute of the given type, throw an exception\r
+                       if (attributes.Length > 1) {\r
+                               throw new System.Reflection.AmbiguousMatchException (\r
+                                       Locale.GetText ("<element> has more than one attribute of type <attribute_type>"));\r
+                       }\r
+\r
+                       if (attributes.Length < 1) \r
+                               return null;\r
+\r
+                       // tested above for '> 1' and and '< 1', so only '== 1' is left,\r
+                       // i.e. we found the attribute\r
+                       \r
+                       return (System.Attribute) attributes[0];\r
+               }\r
+\r
+               private static void CheckAncestry (Type attribute_type)\r
+               {\r
+                       // attribute_type must be derived from type System.Attribute\r
+                       Type t = typeof (System.Attribute);\r
+                       \r
+                       /* fixme: thgi does not work for target monolib2\r
+                       if (!attribute_type.IsSubclassOf (t))\r
+                       {\r
+                               throw new ArgumentException ("Parameter is not a type derived from System.Attribute", "attribute_type");\r
+                       }\r
+                       */\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.ParameterInfo element,\r
+                                                           Type attribute_type)\r
+               {\r
+                       return GetCustomAttribute (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.MemberInfo element,\r
+                                                           Type attribute_type)\r
+               {\r
+                       return GetCustomAttribute (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.Assembly element,\r
+                                                           Type attribute_type)\r
+               {\r
+                       return GetCustomAttribute (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.Module element,\r
+                                                           Type attribute_type)\r
+               {\r
+                       return GetCustomAttribute (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.Module element,\r
+                                                           Type attribute_type, bool inherit)\r
+               {\r
+                       // neither parameter is allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // attribute_type must be derived from type System.Attribute\r
+                       CheckAncestry (attribute_type);\r
+\r
+                       // Module inheritance hierarchies CAN NOT be searched for attributes, so the second\r
+                       // parameter of GetCustomAttributes () is INGNORED.\r
+                       object[] attributes = element.GetCustomAttributes (attribute_type, inherit);\r
+\r
+                       return FindAttribute (attributes);\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.Assembly element,\r
+                                                           Type attribute_type, bool inherit)\r
+               {\r
+                       // neither parameter is allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // attribute_type must be derived from type System.Attribute\r
+                       CheckAncestry (attribute_type);\r
+\r
+                       // Assembly inheritance hierarchies CAN NOT be searched for attributes, so the second\r
+                       // parameter of GetCustomAttributes () is INGNORED.\r
+                       object[] attributes = element.GetCustomAttributes (attribute_type, inherit);\r
+\r
+                       return FindAttribute (attributes);\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.ParameterInfo element,\r
+                                                           Type attribute_type, bool inherit)\r
+               {\r
+                       // neither parameter is allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // attribute_type must be derived from type System.Attribute\r
+                       CheckAncestry (attribute_type);\r
+\r
+                       // ParameterInfo inheritance hierarchies CAN NOT be searched for attributes, so the second\r
+                       // parameter of GetCustomAttributes () is INGNORED.\r
+                       object[] attributes = element.GetCustomAttributes (attribute_type, inherit);\r
+\r
+                       return FindAttribute (attributes);\r
+               }\r
+\r
+               public static Attribute GetCustomAttribute (System.Reflection.MemberInfo element,\r
+                                                           Type attribute_type, bool inherit)\r
+               {\r
+                       // neither parameter is allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // attribute_type must be derived from type System.Attribute\r
+                       CheckAncestry (attribute_type);\r
+\r
+                       // MemberInfo inheritance hierarchies can be searched for attributes, so the second\r
+                       // parameter of GetCustomAttributes () is respected.\r
+                       object[] attributes = element.GetCustomAttributes (attribute_type, inherit);\r
+\r
+                       return FindAttribute (attributes);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element)\r
+               {\r
+                       return System.Attribute.GetCustomAttributes (element, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element)\r
+               {\r
+                       return System.Attribute.GetCustomAttributes (element, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element){\r
+                       return System.Attribute.GetCustomAttributes (element, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Module element){\r
+                       return System.Attribute.GetCustomAttributes (element, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element,\r
+                                                              Type attribute_type)\r
+               {\r
+                       return System.Attribute.GetCustomAttributes (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Module element,\r
+                                                              Type attribute_type)\r
+               {\r
+                       return System.Attribute.GetCustomAttributes (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element,\r
+                                                              Type attribute_type)\r
+               {\r
+                       return System.Attribute.GetCustomAttributes (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element,\r
+                                                              Type attribute_type)\r
+               {\r
+                       return System.Attribute.GetCustomAttributes (element, attribute_type, true);\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element,\r
+                                                              Type attribute_type, bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes;\r
+\r
+                       attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               attribute_type, inherit);\r
+\r
+                       return attributes;\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element,\r
+                                                              Type attribute_type, bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               attribute_type, inherit);\r
+\r
+                       return attributes;\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Module element,\r
+                                                              Type attribute_type, bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               attribute_type, inherit);\r
+\r
+                       return attributes;\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element,\r
+                                                              Type attribute_type, bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, attribute_type);\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               attribute_type, inherit);\r
+\r
+                       return attributes;\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Module element,\r
+                                                              bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, typeof (System.Attribute));\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               inherit);\r
+\r
+                       return attributes;\r
+               }\r
+               \r
+               public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element,\r
+                                                              bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, typeof (System.Attribute));\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               inherit);\r
+\r
+                       return attributes;\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element,\r
+                                                              bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, typeof (System.Attribute));\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               inherit);\r
+\r
+                       return attributes;\r
+               }\r
+\r
+               public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element,\r
+                                                              bool inherit)\r
+               {\r
+                       // element parameter is not allowed to be null\r
+                       CheckParameters (element, typeof (System.Attribute));\r
+\r
+                       // make a properly typed array to return containing the custom attributes\r
+                       System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes (\r
+                               inherit);\r
+\r
+                       return attributes;\r
+               }\r
+\r
+               public override int GetHashCode ()\r
+               {\r
+                       return ((Object) this).GetHashCode ();\r
+               }\r
+               \r
+               public virtual bool IsDefaultAttribute ()\r
+               {\r
+                       // Derived classes should override this default behaviour as appropriate\r
+                       return false;\r
+               }\r
+               \r
+               public static bool IsDefined (System.Reflection.Module element, Type attribute_type)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (element, attribute_type).Length > 0);\r
+               }\r
+\r
+               public static bool IsDefined (System.Reflection.ParameterInfo element, Type attribute_type)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (element, attribute_type).Length > 0);\r
+               }\r
+\r
+               public static bool IsDefined (System.Reflection.MemberInfo element, Type attribute_type)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (element, attribute_type).Length > 0);\r
+               }\r
+\r
+               public static bool IsDefined (System.Reflection.Assembly element, Type attribute_type)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (element, attribute_type).Length > 0);\r
+               }\r
+\r
+               public static bool IsDefined (System.Reflection.MemberInfo element, Type attribute_type,\r
+                                             bool inherit)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (\r
+                               element, attribute_type, inherit).Length > 0);\r
+               }\r
+\r
+               public static bool IsDefined (System.Reflection.Assembly element, Type attribute_type,\r
+                                             bool inherit)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (\r
+                               element, attribute_type, inherit).Length > 0);\r
+               }\r
+\r
+               public static bool IsDefined (System.Reflection.Module element, Type attribute_type,\r
+                                             bool inherit)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (\r
+                               element, attribute_type, inherit).Length > 0);\r
+               }\r
+\r
+               public static bool IsDefined (System.Reflection.ParameterInfo element,\r
+                                             Type attribute_type, bool inherit)\r
+               {\r
+                       return (System.Attribute.GetCustomAttributes (\r
+                               element, attribute_type, inherit).Length > 0);\r
+               }\r
+               \r
+               public virtual bool Match (object obj)\r
+               {\r
+                       // default action is the same as Equals.\r
+                       // Derived classes should override as appropriate\r
+                       \r
+                       return this.Equals (obj);\r
+               }\r
+\r
+               public override bool Equals (object obj)\r
+               {\r
+                       if (obj == null || !(obj is Attribute))\r
+                               return false;\r
+\r
+                       return ((Attribute) obj) == this;\r
+               }\r
+       }\r
+}\r