Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Make DBNull serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorHofer committed Sep 7, 2017
1 parent f3dfbf5 commit db966f6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,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/mscorlib/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, null, null);
}

public override string ToString()
Expand Down
58 changes: 58 additions & 0 deletions src/mscorlib/shared/System/UnitySerializationHolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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;
using System.Reflection;
using System.Diagnostics.Contracts;

namespace System
{
// Holds classes (Empty, Null, Missing) for which we guarantee that there is only ever one instance of.
#if CORECLR
internal
#else
public // On CoreRT, this must be public because of the Reflection.Core/CoreLib divide and the need to whitelist past the ReflectionBlock.
#endif
class UnitySerializationHolder : ISerializable, IObjectReference
{
internal const int NullUnity = 0x0002;
const string DataName = "Data";
const string UnityTypeName = "UnityType";
const string AssemblyNameName = "AssemblyName";

private readonly string _data;
private readonly string _assemblyName;
private int _unityType;

public static void GetUnitySerializationInfo(SerializationInfo info, int unityType, string data, Assembly assembly)
{
// 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.)

info.SetType(typeof(UnitySerializationHolder));
info.AddValue(DataName, data, typeof(string));
info.AddValue(UnityTypeName, unityType);
info.AddValue(AssemblyNameName, assembly?.FullName ?? string.Empty);
}

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

_unityType = info.GetInt32(UnityTypeName);
_data = info.GetString(DataName);
_assemblyName = info.GetString(AssemblyNameName);
}

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

public virtual object GetRealObject(StreamingContext context) => DBNull.Value;
}
}

0 comments on commit db966f6

Please sign in to comment.