Skip to content

Commit 55b90b7

Browse files
morganbrkrwq
authored andcommitted
ISerializable cleanup (dotnet#20220)
* Changes to throw PlatformNotSupportedException from ISerializable.GetObjectData and serialization constructors on non-serializable types. Also removes private serialization constructors and some unneeded code that was used to support serializing non-serializable types. A few tests testing GetObjectData implementations are also removed. * Address code review comments. * Change exceptions' GetObjectData to just call base rather than throw. This makes them behave consistently with exceptions that didn't override GetObjectData.
1 parent 59e6810 commit 55b90b7

File tree

120 files changed

+342
-1090
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+342
-1090
lines changed

src/Common/src/System/Net/NetworkInformation/NetworkInformationException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public NetworkInformationException(int errorCode) : base(errorCode)
3636

3737
protected NetworkInformationException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext)
3838
{
39+
throw new PlatformNotSupportedException();
3940
}
4041

4142
internal NetworkInformationException(SocketError socketError) : base((int)socketError)

src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinderException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public RuntimeBinderException(string message, Exception innerException)
4949
protected RuntimeBinderException(SerializationInfo info, StreamingContext context)
5050
: base(info, context)
5151
{
52+
throw new PlatformNotSupportedException();
5253
}
5354
}
5455
}

src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/RuntimeBinderInternalCompilerException.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public RuntimeBinderInternalCompilerException(string message, Exception innerExc
4949
protected RuntimeBinderInternalCompilerException(SerializationInfo info, StreamingContext context)
5050
: base(info, context)
5151
{
52+
throw new PlatformNotSupportedException();
5253
}
5354
}
5455
}

src/Microsoft.Win32.Primitives/src/System/ComponentModel/Win32Exception.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,11 @@ public Win32Exception(string message, Exception innerException) : base(message,
6363

6464
protected Win32Exception(SerializationInfo info, StreamingContext context) : base(info, context)
6565
{
66-
nativeErrorCode = info.GetInt32(nameof(NativeErrorCode));
66+
throw new PlatformNotSupportedException();
6767
}
6868

6969
public override void GetObjectData(SerializationInfo info, StreamingContext context)
7070
{
71-
if (info == null)
72-
{
73-
throw new ArgumentNullException(nameof(info));
74-
}
75-
76-
info.AddValue(nameof(NativeErrorCode), nativeErrorCode);
7771
base.GetObjectData(info, context);
7872
}
7973

src/Microsoft.Win32.Primitives/tests/Win32Exception.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,5 @@ public static void InstantiateExceptionWithLongErrorString()
101101
Assert.Equal(expected: "Unknown error (0x23)", actual: ex.Message);
102102
}
103103
}
104-
105-
[Fact]
106-
public static void GetObjectData_InvalidArgs_Throws()
107-
{
108-
var e = new Win32Exception();
109-
AssertExtensions.Throws<ArgumentNullException>("info", () => e.GetObjectData(null, default(StreamingContext)));
110-
}
111104
}
112105
}

src/System.CodeDom/src/System/CodeDom/CodeNamespace.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public CodeNamespace(string name)
2929
Name = name;
3030
}
3131

32-
private CodeNamespace(SerializationInfo info, StreamingContext context) { }
33-
3432
public CodeTypeDeclarationCollection Types
3533
{
3634
get

src/System.CodeDom/src/System/CodeDom/Compiler/CodeDomProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ private ICodeParser CreateParserHelper()
215215
private sealed class ConfigurationErrorsException : SystemException
216216
{
217217
public ConfigurationErrorsException(string message) : base(message) { }
218-
public ConfigurationErrorsException(SerializationInfo info, StreamingContext context) : base(info, context) { }
218+
public ConfigurationErrorsException(SerializationInfo info, StreamingContext context) : base(info, context)
219+
{
220+
throw new PlatformNotSupportedException();
221+
}
219222
}
220223
}
221224
}

src/System.CodeDom/tests/CodeObjects/CodeNamespaceTests.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ public void Ctor_String(string name)
3232
Assert.Empty(codeNamespace.Comments);
3333
}
3434

35-
[Fact]
36-
[SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Cannot do internal Reflection on framework types.")]
37-
public void Ctor_SerializationInfo_StreamingContext()
38-
{
39-
ConstructorInfo constructor = typeof(CodeNamespace).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
40-
var codeNamespace = (CodeNamespace)constructor.Invoke(new object[] { default(SerializationInfo), default(StreamingContext) });
41-
Assert.Empty(codeNamespace.Name);
42-
Assert.Empty(codeNamespace.Types);
43-
Assert.Empty(codeNamespace.Imports);
44-
Assert.Empty(codeNamespace.Comments);
45-
}
46-
4735
[Theory]
4836
[MemberData(nameof(String_TestData))]
4937
public void Name_Set_Get_ReturnsExpected(string value)

src/System.Collections.Specialized/src/System/Collections/Specialized/NameObjectCollectionBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace System.Collections.Specialized
2121
/// and <see cref='System.Object' qualify='true'/> values that can be accessed either with the hash code of
2222
/// the key or with the index.</para>
2323
/// </devdoc>
24+
[Serializable]
2425
public abstract class NameObjectCollectionBase : ICollection, ISerializable, IDeserializationCallback
2526
{
2627
// const names used for serialization

src/System.Collections/src/System/Collections/Generic/LinkedList.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,7 @@ internal Enumerator(LinkedList<T> list)
563563

564564
private Enumerator(SerializationInfo info, StreamingContext context)
565565
{
566-
_siInfo = info;
567-
_list = null;
568-
_version = 0;
569-
_node = null;
570-
_current = default(T);
571-
_index = 0;
566+
throw new PlatformNotSupportedException();
572567
}
573568

574569
public T Current
@@ -630,15 +625,7 @@ public void Dispose()
630625

631626
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
632627
{
633-
if (info == null)
634-
{
635-
throw new ArgumentNullException(nameof(info));
636-
}
637-
638-
info.AddValue(LinkedListName, _list);
639-
info.AddValue(VersionName, _version);
640-
info.AddValue(CurrentValueName, _current);
641-
info.AddValue(IndexName, _index);
628+
throw new PlatformNotSupportedException();
642629
}
643630

644631
void IDeserializationCallback.OnDeserialization(Object sender)

0 commit comments

Comments
 (0)