[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[mono.git] / mcs / class / corlib / System.Runtime.Serialization.Formatters.Binary / BinaryFormatter.cs
index 57f770516d2ce76cc1d12bda500da9410d38a447..4d4756e150d5a16689db6d7109feaaad950c38eb 100644 (file)
@@ -37,7 +37,11 @@ using System.Security.Permissions;
 namespace System.Runtime.Serialization.Formatters.Binary {
 
        [ComVisible (true)]
-       public sealed class BinaryFormatter : IRemotingFormatter, IFormatter 
+       public sealed class BinaryFormatter :
+#if !FULL_AOT_RUNTIME
+                                               IRemotingFormatter,
+#endif
+                                               IFormatter
        {
                private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Simple;
                private SerializationBinder binder;
@@ -48,7 +52,7 @@ namespace System.Runtime.Serialization.Formatters.Binary {
                
                public BinaryFormatter()
                {
-                       surrogate_selector=null;
+                       surrogate_selector=DefaultSurrogateSelector;
                        context=new StreamingContext(StreamingContextStates.All);
                }
                
@@ -57,7 +61,17 @@ namespace System.Runtime.Serialization.Formatters.Binary {
                        surrogate_selector=selector;
                        this.context=context;
                }
+               
 
+               // Deserializing objects of type Dictionary<,> List<> and friends does not work in a CoreCLR sandbox, because
+               // the default deserialization code uses reflection to do its job, and the fields being reflected on live in mscorlib.dll.
+               // DefaultSurrogateSelector enables embedders to provide an alternative method of deserializing specific types in a way
+               // that does not violate the CoreCLR rules. See https://gist.github.com/878267 for some actual code that provides CoreCLR safe 
+               // deserialization code for List<> and Dictionary<,>.
+               // DefaultSurrogateSelector is private, and needs to be set by the embedder trough reflection, so we do not expose any public
+               // API point that is not present in .NET
+               static ISurrogateSelector DefaultSurrogateSelector { get; set; }
+               
                public FormatterAssemblyStyle AssemblyFormat
                {
                        get {