Fix pragma warning restore (dotnet/coreclr#26389)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / UnitySerializationHolder.cs
blobac90cf152ab58942f5a29e6ceb0f31f70f5a54aa
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;
7 namespace System
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 // 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;
21 /// <summary>
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).
25 /// </summary>
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
38 if (info == null)
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.
61 return DBNull.Value;