This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Make DBNull serializable #13845
Merged
Merged
Make DBNull serializable #13845
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
db966f6
Make DBNull serializable
ViktorHofer f2706ae
PR feedback
ViktorHofer 29408dc
More comments and remove of corert special case
ViktorHofer b142440
Revert access modifier change and update its comment
ViktorHofer 35f8867
Add exception handling for other input and add comments
ViktorHofer 423d10d
Remove unused params
ViktorHofer b9a716a
PR feedback
ViktorHofer d19b3ac
Adding serializable attribute to type
ViktorHofer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to add the string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, it's already there by mistake...