2010-02-26 Robert Jordan <robertj@gmx.net>
authorRobert Jordan <robertj@gmx.net>
Fri, 26 Feb 2010 20:24:43 +0000 (20:24 -0000)
committerRobert Jordan <robertj@gmx.net>
Fri, 26 Feb 2010 20:24:43 +0000 (20:24 -0000)
* ObjectReader.cs (ReadType, GetDeserializationType):
Add throwOnError parameter that specifies whether to throw an
  exception if the type/assembly is not found.

* ObjectReader.cs (ReadTypeMetadata): Ignore unresolvable
  types while reading the type info. Fixes #430583.

* ObjectReader.cs (GetDeserializationType): Rethrow caught
  exceptions as SerializationExceptions.

svn path=/trunk/mcs/; revision=152579

mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ChangeLog
mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs

index ff28924478069da4bfc830d7d3cc58d7bfa8e01f..3975be4a7ecdb09136a9928055521b22384aaf27 100644 (file)
@@ -1,3 +1,15 @@
+2010-02-26  Robert Jordan  <robertj@gmx.net>
+
+       * ObjectReader.cs (ReadType, GetDeserializationType):
+       Add throwOnError parameter that specifies whether to throw an
+       exception if the type/assembly is not found.
+
+       * ObjectReader.cs (ReadTypeMetadata): Ignore unresolvable
+       types while reading the type info. Fixes #430583.
+
+       * ObjectReader.cs (GetDeserializationType): Rethrow caught
+       exceptions as SerializationExceptions.
+
 2009-10-31  Sebastien Pouliot  <sebastien@ximian.com>
 
        * BinaryCommon.cs: Disable MONO_REFLECTION_SERIALIZER override 
index ec6994490ad7cabd823638bada5bf608c8650d8b..8dbe14da3770b771f78887ec3fe79c08e64ce173 100644 (file)
@@ -621,8 +621,13 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                for (int n=0; n<fieldCount; n++)
                                        codes [n] = (TypeTag) reader.ReadByte ();
        
-                               for (int n=0; n<fieldCount; n++)
-                                       types [n] = ReadType (reader, codes[n]);
+                               for (int n=0; n<fieldCount; n++) {
+                                       Type t = ReadType (reader, codes[n], false);
+                                       // The field's type could not be resolved: assume it is an object.
+                                       if (t == null)
+                                               t = typeof (object);
+                                       types [n] = t;
+                               }
                        }
                        
                        // Gets the type
@@ -815,6 +820,11 @@ namespace System.Runtime.Serialization.Formatters.Binary
                }
 
                private Type GetDeserializationType (long assemblyId, string className)
+               {
+                       return GetDeserializationType (assemblyId, className, true);
+               }
+               
+               private Type GetDeserializationType (long assemblyId, string className, bool throwOnError)
                {
                        Type t;
                        string assemblyName = (string)_registeredAssemblies[assemblyId];
@@ -824,15 +834,32 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                if (t != null)
                                        return t;
                        }
-                               
-                       Assembly assembly = Assembly.Load (assemblyName);
-                       t = assembly.GetType (className, true);
+
+                       Assembly assembly;
+                       try {
+                               assembly = Assembly.Load (assemblyName);
+                       } catch (Exception ex) {
+                               if (!throwOnError)
+                                       return null;
+                               throw new SerializationException (String.Format ("Couldn't find assembly '{0}'", assemblyName), ex);
+                       }
+
+                       t = assembly.GetType (className);
                        if (t != null)
                                return t;
-                       throw new SerializationException ("Couldn't find type '" + className + "'.");
+
+                       if (!throwOnError)
+                               return null;
+
+                       throw new SerializationException (String.Format ("Couldn't find type '{0}' in assembly '{1}'", className, assemblyName));
                }
 
                public Type ReadType (BinaryReader reader, TypeTag code)
+               {
+                       return ReadType (reader, code, true);
+               }
+               
+               public Type ReadType (BinaryReader reader, TypeTag code, bool throwOnError)
                {
                        switch (code)
                        {
@@ -865,7 +892,7 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                {
                                        string name = reader.ReadString ();
                                        long asmid = (long) reader.ReadUInt32();
-                                       return GetDeserializationType (asmid, name);
+                                       return GetDeserializationType (asmid, name, throwOnError);
                                }
 
                                case TypeTag.ArrayOfObject: