Merge pull request #1229 from andreiagaita/master
[mono.git] / mcs / class / corlib / System.Reflection / CustomAttributeTypedArgument.cs
index 27eaef9ace1fd9af7e28ea3a72c43313e4c667eb..c73e32c9a949f817af86a8ae42ed854606be4613 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
 
 using System;
 using System.Runtime.InteropServices;
+using System.Collections.ObjectModel;
 
 namespace System.Reflection {
 
@@ -40,11 +40,39 @@ namespace System.Reflection {
                Type argumentType;
                object value;
 
-               internal CustomAttributeTypedArgument (Type argumentType, object value)
+#if NET_4_0
+               public
+#endif
+               CustomAttributeTypedArgument (Type argumentType, object value)
                {
+                       if (argumentType == null)
+                               throw new ArgumentNullException ("argumentType");
+
                        this.argumentType = argumentType;
                        this.value = value;
+
+                       // MS seems to convert arrays into a ReadOnlyCollection
+                       if (value is Array) {
+                               Array a = (Array)value;
+
+                               Type etype = a.GetType ().GetElementType ();
+                               CustomAttributeTypedArgument[] new_value = new CustomAttributeTypedArgument [a.GetLength (0)];
+                               for (int i = 0; i < new_value.Length; ++i)
+                                       new_value [i] = new CustomAttributeTypedArgument (etype, a.GetValue (i));
+                               this.value = new ReadOnlyCollection <CustomAttributeTypedArgument> (new_value);
+                       }
+               }
+               
+#if NET_4_0
+               public CustomAttributeTypedArgument (object value)
+               {
+                       if (value == null)
+                               throw new ArgumentNullException ("value");
+
+                       this.argumentType = value.GetType ();
+                       this.value = value;
                }
+#endif
 
                public Type ArgumentType {
                        get {
@@ -60,7 +88,7 @@ namespace System.Reflection {
 
                public override string ToString ()
                {
-                       string val = value.ToString ();
+                       string val = value != null ? value.ToString () : String.Empty;
                        if (argumentType == typeof (string))
                                return "\"" + val + "\"";
                        if (argumentType == typeof (Type)) 
@@ -70,9 +98,32 @@ namespace System.Reflection {
 
                        return val;
                }
+
+               public override bool Equals (object obj)
+               {
+                       if (!(obj is CustomAttributeTypedArgument))
+                               return false;
+                       CustomAttributeTypedArgument other = (CustomAttributeTypedArgument) obj;
+                       return  other.argumentType == argumentType &&
+                               value != null ? value.Equals (other.value) : (object) other.value == null;
+               }
+
+               public override int GetHashCode ()
+               {
+                       return (argumentType.GetHashCode () << 16) + (value != null ? value.GetHashCode () : 0);
+               }
+
+               public static bool operator == (CustomAttributeTypedArgument left, CustomAttributeTypedArgument right)
+               {
+                       return left.Equals (right);
+               }
+
+               public static bool operator != (CustomAttributeTypedArgument left, CustomAttributeTypedArgument right)
+               {
+                       return !left.Equals (right);
+               }
        }
 
 }
 
-#endif