Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request dotnet/coreclr#13845 from ViktorHofer/DBNull-Seria…
Browse files Browse the repository at this point in the history
…lization

Make DBNull serializable

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
ViktorHofer authored and dotnet-bot committed Sep 9, 2017
1 parent 668315c commit 8469690
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\UnauthorizedAccessException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\UnhandledExceptionEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\UnhandledExceptionEventHandler.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\UnitySerializationHolder.cs"/>
<Compile Include="$(MSBuildThisFileDirectory)System\ValueTuple.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Version.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Void.cs" />
Expand Down
10 changes: 8 additions & 2 deletions src/System.Private.CoreLib/shared/System/DBNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@

namespace System
{
[Serializable]
public sealed class DBNull : ISerializable, IConvertible
{
private DBNull()
{
}


private DBNull(SerializationInfo info, StreamingContext context)
{
throw new NotSupportedException(SR.NotSupported_DBNullSerial);
}

public static readonly DBNull Value = new DBNull();

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new PlatformNotSupportedException();
UnitySerializationHolder.GetUnitySerializationInfo(info, UnitySerializationHolder.NullUnity);
}

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.Serialization;

namespace System
{
/// <summary>
/// Holds Null class for which we guarantee that there is only ever one instance of.
/// This only exists for compatibility with .NET Framework.
/// </summary>
[Serializable]
#if CORECLR
internal
#else
public // On CoreRT this must be public.
#endif
sealed class UnitySerializationHolder : ISerializable, IObjectReference
{
internal const int NullUnity = 0x0002;
private readonly int _unityType;
private readonly string _data;

/// <summary>
/// A helper method that returns the SerializationInfo that a class utilizing
/// UnitySerializationHelper should return from a call to GetObjectData. It contains
/// the unityType (defined above) and any optional data (used only for the reflection types).
/// </summary>
internal static void GetUnitySerializationInfo(SerializationInfo info, int unityType)
{
info.SetType(typeof(UnitySerializationHolder));
info.AddValue("Data", null, typeof(string));
info.AddValue("UnityType", unityType);
info.AddValue("AssemblyName", string.Empty);
}

public UnitySerializationHolder(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

// We are ignoring any other serialization input as we are only concerned about DBNull.
// We also store data and use it for erorr logging.
_unityType = info.GetInt32("UnityType");
_data = info.GetString("Data");
}

public void GetObjectData(SerializationInfo info, StreamingContext context) =>
throw new NotSupportedException(SR.NotSupported_UnitySerHolder);

public object GetRealObject(StreamingContext context)
{
// We are only support deserializing DBNull and throwing for everything else.
if (_unityType != NullUnity)
{
throw new ArgumentException(SR.Format(SR.Argument_InvalidUnity, _data ?? "UnityType"));
}

// We are always returning the same DBNull instance.
return DBNull.Value;
}
}
}

0 comments on commit 8469690

Please sign in to comment.