2010-03-27 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Reflection / CustomAttributeData.cs
index 57a9526c331a3f0d1a438ea1f5cf57d946f2ec6f..74eaf7d04c158feaf5b91b3a221e839d0ee6ddfd 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Zoltan Varga (vargaz@gmail.com)
+//   Carlos Alberto Cortez (calberto.cortez@gmail.com)
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
 //
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System;
 using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using System.Text;
 
 namespace System.Reflection {
 
-       public sealed class CustomAttributeData {
+       [ComVisible (true)]
+       [Serializable]
+#if NET_4_0
+       public
+#else
+       public sealed
+#endif
+       class CustomAttributeData {
+               ConstructorInfo ctorInfo;
+               IList<CustomAttributeTypedArgument> ctorArgs;
+               IList<CustomAttributeNamedArgument> namedArgs;
+
+#if NET_4_0
+               protected CustomAttributeData ()
+               {
+               }
+#endif
+
+               internal CustomAttributeData (ConstructorInfo ctorInfo, object [] ctorArgs, object [] namedArgs)
+               {
+                       this.ctorInfo = ctorInfo;
+                       
+                       this.ctorArgs = Array.AsReadOnly<CustomAttributeTypedArgument> 
+                               (ctorArgs != null ? UnboxValues<CustomAttributeTypedArgument> (ctorArgs) : new CustomAttributeTypedArgument [0]);
+                       
+                       this.namedArgs = Array.AsReadOnly<CustomAttributeNamedArgument> 
+                               (namedArgs != null ? UnboxValues<CustomAttributeNamedArgument> (namedArgs) : new CustomAttributeNamedArgument [0]);
+               }
 
-               [MonoTODO]
-               public ConstructorInfo Constructor {
+               [ComVisible (true)]
+               public
+#if NET_4_0
+               virtual
+#endif
+               ConstructorInfo Constructor {
                        get {
-                               throw new NotImplementedException ();
+                               return ctorInfo;
                        }
                }
 
-               [MonoTODO]
-               [CLSCompliant (false)]
-               public IList<CustomAttributeTypedArgument> ConstructorArguments {
+               [ComVisible (true)]
+               public
+#if NET_4_0
+               virtual
+#endif
+               IList<CustomAttributeTypedArgument> ConstructorArguments {
                        get {
-                               throw new NotImplementedException ();
+                               return ctorArgs;
                        }
                }
 
-               [MonoTODO]
-               [CLSCompliant (false)]
-               public IList<CustomAttributeNamedArgument> NamedArguments {
+               public
+#if NET_4_0
+               virtual
+#endif
+               IList<CustomAttributeNamedArgument> NamedArguments {
                        get {
-                               throw new NotImplementedException ();
+                               return namedArgs;
                        }
                }
 
-               [MonoTODO]
-               [CLSCompliant (false)]
-               public static IList<CustomAttributeData> GetCustomAttributes (Assembly yarget) {
-                       throw new NotImplementedException ();
+               public static IList<CustomAttributeData> GetCustomAttributes (Assembly target) {
+                       return MonoCustomAttrs.GetCustomAttributesData (target);
                }
 
-               [MonoTODO]
-               [CLSCompliant (false)]
                public static IList<CustomAttributeData> GetCustomAttributes (MemberInfo target) {
-                       throw new NotImplementedException ();
+                       return MonoCustomAttrs.GetCustomAttributesData (target);
                }
 
-               [MonoTODO]
-               [CLSCompliant (false)]
                public static IList<CustomAttributeData> GetCustomAttributes (Module target) {
-                       throw new NotImplementedException ();
+                       return MonoCustomAttrs.GetCustomAttributesData (target);
                }
 
-               [MonoTODO]
-               [CLSCompliant (false)]
                public static IList<CustomAttributeData> GetCustomAttributes (ParameterInfo target) {
-                       throw new NotImplementedException ();
+                       return MonoCustomAttrs.GetCustomAttributesData (target);
+               }
+
+               public override string ToString ()
+               {
+                       StringBuilder sb = new StringBuilder ();
+
+                       sb.Append ("[" + ctorInfo.DeclaringType.FullName + "(");
+                       for (int i = 0; i < ctorArgs.Count; i++) {
+                               sb.Append (ctorArgs [i].ToString ());
+                               if (i + 1 < ctorArgs.Count)
+                                       sb.Append (", ");
+                       }
+
+                       if (namedArgs.Count > 0)
+                               sb.Append (", ");
+                       
+                       for (int j = 0; j < namedArgs.Count; j++) {
+                               sb.Append (namedArgs [j].ToString ());
+                               if (j + 1 < namedArgs.Count)
+                                       sb.Append (", ");
+                       }
+                       sb.AppendFormat (")]");
+
+                       return sb.ToString ();
+               }
+
+               static T [] UnboxValues<T> (object [] values)
+               {
+                       T [] retval = new T [values.Length];
+                       for (int i = 0; i < values.Length; i++)
+                               retval [i] = (T) values [i];
+
+                       return retval;
+               }
+
+               public override bool Equals (object obj)
+               {
+                       CustomAttributeData other = obj as CustomAttributeData;
+                       if (other == null || other.ctorInfo != ctorInfo ||
+                           other.ctorArgs.Count != ctorArgs.Count ||
+                           other.namedArgs.Count != namedArgs.Count)
+                               return false;
+                       for (int i = 0; i < ctorArgs.Count; i++)
+                               if (ctorArgs [i].Equals (other.ctorArgs [i]))
+                                       return false;
+                       for (int i = 0; i < namedArgs.Count; i++) {
+                               bool matched = false;
+                               for (int j = 0; j < other.namedArgs.Count; j++)
+                                       if (namedArgs [i].Equals (other.namedArgs [j])) {
+                                               matched = true;
+                                               break;
+                                       }
+                               if (!matched)
+                                       return false;
+                       }
+                       return true;
+               }
+
+               public override int GetHashCode ()
+               {
+                       int ret = ctorInfo.GetHashCode () << 16;
+                       // argument order-dependent
+                       for (int i = 0; i < ctorArgs.Count; i++)
+                               ret += ret ^ 7 + ctorArgs [i].GetHashCode () << (i * 4);
+                       // argument order-independent
+                       for (int i = 0; i < namedArgs.Count; i++)
+                               ret += (namedArgs [i].GetHashCode () << 5);
+                       return ret;
                }
        }
 
 }
 
-#endif
-