|
| 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. |
| 4 | + |
| 5 | +using System.Runtime.Serialization; |
| 6 | + |
| 7 | +namespace System |
| 8 | +{ |
| 9 | + /// <summary> |
| 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. |
| 12 | + /// </summary> |
| 13 | + [Serializable] |
| 14 | +#if CORECLR |
| 15 | + internal |
| 16 | +#else |
| 17 | + public // On CoreRT this must be public. |
| 18 | +#endif |
| 19 | + sealed class UnitySerializationHolder : ISerializable, IObjectReference |
| 20 | + { |
| 21 | + internal const int NullUnity = 0x0002; |
| 22 | + private readonly int _unityType; |
| 23 | + private readonly string _data; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// A helper method that returns the SerializationInfo that a class utilizing |
| 27 | + /// UnitySerializationHelper should return from a call to GetObjectData. It contains |
| 28 | + /// the unityType (defined above) and any optional data (used only for the reflection types). |
| 29 | + /// </summary> |
| 30 | + internal static void GetUnitySerializationInfo(SerializationInfo info, int unityType) |
| 31 | + { |
| 32 | + info.SetType(typeof(UnitySerializationHolder)); |
| 33 | + info.AddValue("Data", null, typeof(string)); |
| 34 | + info.AddValue("UnityType", unityType); |
| 35 | + info.AddValue("AssemblyName", string.Empty); |
| 36 | + } |
| 37 | + |
| 38 | + public UnitySerializationHolder(SerializationInfo info, StreamingContext context) |
| 39 | + { |
| 40 | + if (info == null) |
| 41 | + { |
| 42 | + throw new ArgumentNullException(nameof(info)); |
| 43 | + } |
| 44 | + |
| 45 | + // We are ignoring any other serialization input as we are only concerned about DBNull. |
| 46 | + // We also store data and use it for erorr logging. |
| 47 | + _unityType = info.GetInt32("UnityType"); |
| 48 | + _data = info.GetString("Data"); |
| 49 | + } |
| 50 | + |
| 51 | + public void GetObjectData(SerializationInfo info, StreamingContext context) => |
| 52 | + throw new NotSupportedException(SR.NotSupported_UnitySerHolder); |
| 53 | + |
| 54 | + public object GetRealObject(StreamingContext context) |
| 55 | + { |
| 56 | + // We are only support deserializing DBNull and throwing for everything else. |
| 57 | + if (_unityType != NullUnity) |
| 58 | + { |
| 59 | + throw new ArgumentException(SR.Format(SR.Argument_InvalidUnity, _data ?? "UnityType")); |
| 60 | + } |
| 61 | + |
| 62 | + // We are always returning the same DBNull instance. |
| 63 | + return DBNull.Value; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments