Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / corlib / System / RuntimeTypeHandle.cs
index 3875173e2fb8e0ce9bea9dd6a8246bb95c964091..45fc6edaff2e43719ff92349156f9bc5230ae9ed 100644 (file)
 
 using System.Runtime.Serialization;
 using System.Runtime.InteropServices;
-
-#if NET_2_0
 using System.Runtime.ConstrainedExecution;
-#endif
 
 namespace System
 {
-#if NET_2_0
        [ComVisible (true)]
-#endif
-       [MonoTODO ("Serialization needs tests")]
        [Serializable]
        public struct RuntimeTypeHandle : ISerializable
        {
@@ -76,10 +70,12 @@ namespace System
                        if (info == null)
                                throw new ArgumentNullException ("info");
 
+                       if (value == IntPtr.Zero)
+                               throw new SerializationException ("Object fields may not be properly initialized");
+
                        info.AddValue ("TypeObj", Type.GetTypeHandle (this), typeof (MonoType));
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
                public override bool Equals (object obj)
                {
@@ -100,14 +96,14 @@ namespace System
                        return value.GetHashCode ();
                }
 
-               public static bool operator == (RuntimeTypeHandle left, RuntimeTypeHandle right)
+               public static bool operator == (RuntimeTypeHandle left, Object right)
                {
-                       return left.Equals (right);
+                       return (right != null) && (right is RuntimeTypeHandle) && left.Equals ((RuntimeTypeHandle)right);
                }
 
-               public static bool operator != (RuntimeTypeHandle left, RuntimeTypeHandle right)
+               public static bool operator != (RuntimeTypeHandle left, Object right)
                {
-                       return !left.Equals (right);
+                       return (right == null) || !(right is RuntimeTypeHandle) || !left.Equals ((RuntimeTypeHandle)right);
                }
 
                public static bool operator == (Object left, RuntimeTypeHandle right)
@@ -122,9 +118,15 @@ namespace System
 
                [CLSCompliant (false)]
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-               public ModuleHandle GetModuleHandle () {
+               public ModuleHandle GetModuleHandle ()
+               {
+                       // Although MS' runtime is crashing here, we prefer throwing an exception.
+                       // The check is needed because Type.GetTypeFromHandle returns null
+                       // for zero handles.
+                       if (value == IntPtr.Zero)
+                               throw new InvalidOperationException ("Object fields may not be properly initialized");
+
                        return Type.GetTypeFromHandle (this).Module.ModuleHandle;
                }
-#endif
        }
 }