1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.Runtime
.Serialization
;
10 /// Holds Null class for which we guarantee that there is only ever one instance of.
11 /// This only exists for compatibility with .NET Framework.
14 // Needs to be public to support binary serialization compatibility
15 public sealed class UnitySerializationHolder
: ISerializable
, IObjectReference
17 internal const int NullUnity
= 0x0002;
18 private readonly int _unityType
;
19 private readonly string? _data
;
22 /// A helper method that returns the SerializationInfo that a class utilizing
23 /// UnitySerializationHelper should return from a call to GetObjectData. It contains
24 /// the unityType (defined above) and any optional data (used only for the reflection types).
26 internal static void GetUnitySerializationInfo(SerializationInfo info
, int unityType
)
28 info
.SetType(typeof(UnitySerializationHolder
));
29 info
.AddValue("Data", null, typeof(string));
30 info
.AddValue("UnityType", unityType
);
31 info
.AddValue("AssemblyName", string.Empty
);
34 #pragma warning disable CA2229 // public for compat
35 public UnitySerializationHolder(SerializationInfo info
, StreamingContext context
)
36 #pragma warning restore CA2229
40 throw new ArgumentNullException(nameof(info
));
43 // We are ignoring any other serialization input as we are only concerned about DBNull.
44 // We also store data and use it for erorr logging.
45 _unityType
= info
.GetInt32("UnityType");
46 _data
= info
.GetString("Data");
49 public void GetObjectData(SerializationInfo info
, StreamingContext context
) =>
50 throw new NotSupportedException(SR
.NotSupported_UnitySerHolder
);
52 public object GetRealObject(StreamingContext context
)
54 // We are only support deserializing DBNull and throwing for everything else.
55 if (_unityType
!= NullUnity
)
57 throw new ArgumentException(SR
.Format(SR
.Argument_InvalidUnity
, _data
?? "UnityType"));
60 // We are always returning the same DBNull instance.