2008-09-24 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / SerializationInfo.cs
index 34946a886a0ae128c8ad5706cfa45e4aee94072b..793328ab94355cf4d752455b39746940b2ff8081 100644 (file)
 //
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Collections;
 
 namespace System.Runtime.Serialization
 {
+#if NET_2_0
+       [System.Runtime.InteropServices.ComVisibleAttribute (true)]
+#endif
        public sealed class SerializationInfo
        {
                Hashtable serialized = new Hashtable ();
+               ArrayList values = new ArrayList ();
+
                string assemblyName; // the assembly being serialized
                string fullTypeName; // the type being serialized.
 
-               [CLSCompliant (false)] IFormatterConverter converter;
+               IFormatterConverter converter;
                
                /* used by the runtime */
                private SerializationInfo (Type type)
@@ -40,16 +68,21 @@ namespace System.Runtime.Serialization
                        fullTypeName = type.FullName;
                        converter = new FormatterConverter ();
 
-                       for (int i = 0; i < len; i++)
+                       for (int i = 0; i < len; i++) {
                                serialized.Add (data [i].Name, data [i]);
+                               values.Add (data [i]);
+                       }
                }
 
                // Constructor
                [CLSCompliant (false)]
                public SerializationInfo (Type type, IFormatterConverter converter)
                {
-                       if (type == null && converter == null)
-                               throw new ArgumentNullException ("Null arguments.");
+                       if (type == null)
+                               throw new ArgumentNullException ("type", "Null argument");
+
+                       if (converter == null)
+                               throw new ArgumentNullException ("converter", "Null argument");
                        
                        this.converter = converter;
                        assemblyName = type.Assembly.FullName;
@@ -87,11 +120,18 @@ namespace System.Runtime.Serialization
                // Methods
                public void AddValue (string name, object value, Type type)
                {
+                       if (name == null)
+                               throw new ArgumentNullException ("name is null");
+                       if (type == null)
+                               throw new ArgumentNullException ("type is null");
+                       
                        if (serialized.ContainsKey (name))
                                throw new SerializationException ("Value has been serialized already.");
                        
-                       SerializationEntry values = new SerializationEntry (name, type, value);
-                       serialized.Add (name, values);
+                       SerializationEntry entry = new SerializationEntry (name, type, value);
+
+                       serialized.Add (name, entry);
+                       values.Add (entry);
                }
 
                public object GetValue (string name, Type type)
@@ -102,13 +142,13 @@ namespace System.Runtime.Serialization
                                throw new ArgumentNullException ("type");
                        if (!serialized.ContainsKey (name))
                                throw new SerializationException ("No element named " + name + " could be found.");
-                                               
-                       SerializationEntry values = (SerializationEntry) serialized [name];
+                                               
+                       SerializationEntry entry = (SerializationEntry) serialized [name];
 
-                       if (values.Value != null && !type.IsAssignableFrom (values.Value.GetType()))
-                               return converter.Convert (values.Value, type);
+                       if (entry.Value != null && !type.IsInstanceOfType (entry.Value))
+                               return converter.Convert (entry.Value, type);
                        else
-                               return values.Value;
+                               return entry.Value;
                }
 
                public void SetType (Type type)
@@ -122,7 +162,7 @@ namespace System.Runtime.Serialization
 
                public SerializationInfoEnumerator GetEnumerator ()
                {
-                       return new SerializationInfoEnumerator (serialized);
+                       return new SerializationInfoEnumerator (values);
                }
                
                public void AddValue (string name, short value)
@@ -151,55 +191,55 @@ namespace System.Runtime.Serialization
                        AddValue (name, value, typeof (System.Boolean));
                }
               
-               public void AddValue (string name, char value)
+               public void AddValue (string name, char value)
                {
                        AddValue (name, value, typeof (System.Char));
                }
 
                [CLSCompliant(false)]
-               public void AddValue (string name, SByte value)
+               public void AddValue (string name, SByte value)
                {
                        AddValue (name, value, typeof (System.SByte));
                }
                
-               public void AddValue (string name, double value)
+               public void AddValue (string name, double value)
                {
                        AddValue (name, value, typeof (System.Double));
                }
                
-               public void AddValue (string name, Decimal value)
+               public void AddValue (string name, Decimal value)
                {
                        AddValue (name, value, typeof (System.Decimal));
                }
                
-               public void AddValue (string name, DateTime value)
+               public void AddValue (string name, DateTime value)
                {
                        AddValue (name, value, typeof (System.DateTime));
                }
                
-               public void AddValue (string name, float value)
+               public void AddValue (string name, float value)
                {
                        AddValue (name, value, typeof (System.Single));
                }
 
                [CLSCompliant(false)]
-               public void AddValue (string name, UInt32 value)
+               public void AddValue (string name, UInt32 value)
                {
                        AddValue (name, value, typeof (System.UInt32));
                }
               
-               public void AddValue (string name, long value)
+               public void AddValue (string name, long value)
                {
                        AddValue (name, value, typeof (System.Int64));
                }
 
                [CLSCompliant(false)]
-               public void AddValue (string name, UInt64 value)
+               public void AddValue (string name, UInt64 value)
                {
                        AddValue (name, value, typeof (System.UInt64));
                }
                
-               public void AddValue (string name, object value)
+               public void AddValue (string name, object value)
                {
                        if (value == null)
                                AddValue (name, value, typeof (System.Object));
@@ -213,19 +253,19 @@ namespace System.Runtime.Serialization
                        return converter.ToBoolean (value);
                }
                
-               public byte GetByte (string name)
+               public byte GetByte (string name)
                {
                        object value = GetValue (name, typeof (System.Byte));
                        return converter.ToByte (value);
                }
                
-               public char GetChar (string name)
+               public char GetChar (string name)
                {
                        object value = GetValue (name, typeof (System.Char));
                        return converter.ToChar (value);
                }
 
-               public DateTime GetDateTime (string name)
+               public DateTime GetDateTime (string name)
                {
                        object value = GetValue (name, typeof (System.DateTime));
                        return converter.ToDateTime (value);
@@ -302,6 +342,7 @@ namespace System.Runtime.Serialization
                }
 
                /* used by the runtime */
+#pragma warning disable 169            
                private SerializationEntry [] get_entries ()
                {
                        SerializationEntry [] res = new SerializationEntry [this.MemberCount];
@@ -312,5 +353,6 @@ namespace System.Runtime.Serialization
                        
                        return res;
                }
+#pragma warning restore 169            
        }
 }