[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / FormatterServices.cs
index 1a03f703682323d190bc3dd8657ce4e1dc2d3874..e2a8702befaeddde56684502e24e96878c699031 100644 (file)
@@ -6,24 +6,48 @@
 //
 // (C) 2002 Ximian, Inc (http://www.ximian.com)
 //
+
+//
+// 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;
 using System.Reflection;
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
 using System.Runtime.Serialization.Formatters;
+using System.Globalization;
 
 namespace System.Runtime.Serialization
 {
-       public sealed class FormatterServices
+       [System.Runtime.InteropServices.ComVisibleAttribute (true)]
+       static
+       public class FormatterServices
        {
                private const BindingFlags fieldFlags = BindingFlags.Public |
                                                        BindingFlags.Instance |
                                                        BindingFlags.NonPublic |
                                                        BindingFlags.DeclaredOnly;
 
-               private FormatterServices ()
-               {
-               }
 
                public static object [] GetObjectData (object obj, MemberInfo [] members)
                {
@@ -74,7 +98,7 @@ namespace System.Runtime.Serialization
                                        throw new SerializationException (msg);
                                }
 
-                               GetFields (t, fields);
+                               GetFields (type, t, fields);
                                t = t.BaseType;
                        }
 
@@ -83,12 +107,19 @@ namespace System.Runtime.Serialization
                        return result;
                }
 
-               private static void GetFields (Type type, ArrayList fields)
+               private static void GetFields (Type reflectedType, Type type, ArrayList fields)
                {
                        FieldInfo [] fs = type.GetFields (fieldFlags);
                        foreach (FieldInfo field in fs)
-                               if (!(field.IsNotSerialized))
-                                       fields.Add (field);
+                               if (!(field.IsNotSerialized)) {
+                                       MonoField mf = field as MonoField;
+                                       if (mf != null && reflectedType != type && !mf.IsPublic) {
+                                               string fname = type.Name + "+" + mf.Name;
+                                               fields.Add (mf.Clone (fname));
+                                       }
+                                       else
+                                               fields.Add (field);
+                               }
                }
 
                public static Type GetTypeFromAssembly (Assembly assem, string name)
@@ -144,7 +175,6 @@ namespace System.Runtime.Serialization
                        return obj;
                }
                
-#if NET_1_1
 
                public static void CheckTypeSecurity (Type t, TypeFilterLevel securityLevel)
                {
@@ -175,6 +205,14 @@ namespace System.Runtime.Serialization
                        
                        return GetUninitializedObject (type);
                }
-#endif
+
+               // This method was introduced in .Net due to a bug serializing objects with circular references
+               // which we don't appear to have, so we just return the same object.
+               // See http://support.microsoft.com/kb/927495/en-us/ in case of doubt.
+               [ComVisible (false)]
+               public static ISerializationSurrogate GetSurrogateForCyclicalReference (ISerializationSurrogate innerSurrogate)
+               {
+                       return innerSurrogate;
+               }
        }
 }