diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.ComponentModel.TypeConverter/src/ILLink/ILLink.Suppressions.xml
index 63a84c9cd0d0f9..d0db9830e3e599 100644
--- a/src/libraries/System.ComponentModel.TypeConverter/src/ILLink/ILLink.Suppressions.xml
+++ b/src/libraries/System.ComponentModel.TypeConverter/src/ILLink/ILLink.Suppressions.xml
@@ -289,5 +289,17 @@
member
M:System.ComponentModel.BindingList`1.get_ItemTypeHasDefaultConstructor
+
+ ILLink
+ IL2026
+ member
+ M:System.ComponentModel.Design.DesigntimeLicenseContextSerializer.Deserialize(System.IO.Stream,System.String,System.ComponentModel.Design.RuntimeLicenseContext)
+
+
+ ILLink
+ IL2026
+ member
+ M:System.ComponentModel.Design.DesigntimeLicenseContextSerializer.Serialize(System.IO.Stream,System.String,System.ComponentModel.Design.DesigntimeLicenseContext)
+
diff --git a/src/libraries/System.Data.Common/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.Data.Common/src/ILLink/ILLink.Suppressions.xml
index 7b29974c298e81..d5d68da0e626ee 100644
--- a/src/libraries/System.Data.Common/src/ILLink/ILLink.Suppressions.xml
+++ b/src/libraries/System.Data.Common/src/ILLink/ILLink.Suppressions.xml
@@ -133,5 +133,17 @@
member
M:System.Data.DataColumn.get_DefaultValue
+
+ ILLink
+ IL2026
+ member
+ M:System.Data.DataSet.DeserializeDataSetSchema(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext,System.Data.SerializationFormat,System.Data.SchemaSerializationMode)
+
+
+ ILLink
+ IL2026
+ member
+ M:System.Data.DataSet.SerializeDataSet(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext,System.Data.SerializationFormat)
+
-
\ No newline at end of file
+
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/ref/System.Runtime.Serialization.Formatters.cs b/src/libraries/System.Runtime.Serialization.Formatters/ref/System.Runtime.Serialization.Formatters.cs
index dc9d5f71b9f3b5..ecfda2d0efd1f3 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/ref/System.Runtime.Serialization.Formatters.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/ref/System.Runtime.Serialization.Formatters.cs
@@ -16,10 +16,12 @@ protected Formatter() { }
public abstract System.Runtime.Serialization.StreamingContext Context { get; set; }
public abstract System.Runtime.Serialization.ISurrogateSelector? SurrogateSelector { get; set; }
[System.ObsoleteAttribute("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0011", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("BinaryFormatter serialization is not trim compatible because the Type of objects being processed cannot be statically discovered.")]
public abstract object Deserialize(System.IO.Stream serializationStream);
protected virtual object? GetNext(out long objID) { throw null; }
protected virtual long Schedule(object? obj) { throw null; }
[System.ObsoleteAttribute("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0011", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("BinaryFormatter serialization is not trim compatible because the Type of objects being processed cannot be statically discovered.")]
public abstract void Serialize(System.IO.Stream serializationStream, object graph);
protected abstract void WriteArray(object obj, string name, System.Type memberType);
protected abstract void WriteBoolean(bool val, string name);
@@ -74,12 +76,13 @@ public static partial class FormatterServices
{
public static void CheckTypeSecurity(System.Type t, System.Runtime.Serialization.Formatters.TypeFilterLevel securityLevel) { }
public static object?[] GetObjectData(object obj, System.Reflection.MemberInfo[] members) { throw null; }
- public static object GetSafeUninitializedObject(System.Type type) { throw null; }
- public static System.Reflection.MemberInfo[] GetSerializableMembers(System.Type type) { throw null; }
- public static System.Reflection.MemberInfo[] GetSerializableMembers(System.Type type, System.Runtime.Serialization.StreamingContext context) { throw null; }
+ public static object GetSafeUninitializedObject([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] System.Type type) { throw null; }
+ public static System.Reflection.MemberInfo[] GetSerializableMembers([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type type) { throw null; }
+ public static System.Reflection.MemberInfo[] GetSerializableMembers([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] System.Type type, System.Runtime.Serialization.StreamingContext context) { throw null; }
public static System.Runtime.Serialization.ISerializationSurrogate GetSurrogateForCyclicalReference(System.Runtime.Serialization.ISerializationSurrogate innerSurrogate) { throw null; }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
public static System.Type? GetTypeFromAssembly(System.Reflection.Assembly assem, string name) { throw null; }
- public static object GetUninitializedObject(System.Type type) { throw null; }
+ public static object GetUninitializedObject([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] System.Type type) { throw null; }
public static object PopulateObjectMembers(object obj, System.Reflection.MemberInfo[] members, object?[] data) { throw null; }
}
public partial interface IFormatter
@@ -88,8 +91,10 @@ public partial interface IFormatter
System.Runtime.Serialization.StreamingContext Context { get; set; }
System.Runtime.Serialization.ISurrogateSelector? SurrogateSelector { get; set; }
[System.ObsoleteAttribute("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0011", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("BinaryFormatter serialization is not trim compatible because the Type of objects being processed cannot be statically discovered.")]
object Deserialize(System.IO.Stream serializationStream);
[System.ObsoleteAttribute("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0011", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("BinaryFormatter serialization is not trim compatible because the Type of objects being processed cannot be statically discovered.")]
void Serialize(System.IO.Stream serializationStream, object graph);
}
public partial interface ISerializationSurrogate
@@ -112,17 +117,23 @@ public ObjectIDGenerator() { }
public partial class ObjectManager
{
public ObjectManager(System.Runtime.Serialization.ISurrogateSelector? selector, System.Runtime.Serialization.StreamingContext context) { }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
public virtual void DoFixups() { }
public virtual object? GetObject(long objectID) { throw null; }
public virtual void RaiseDeserializationEvent() { }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
public void RaiseOnDeserializingEvent(object obj) { }
public virtual void RecordArrayElementFixup(long arrayToBeFixed, int index, long objectRequired) { }
public virtual void RecordArrayElementFixup(long arrayToBeFixed, int[] indices, long objectRequired) { }
public virtual void RecordDelayedFixup(long objectToBeFixed, string memberName, long objectRequired) { }
public virtual void RecordFixup(long objectToBeFixed, System.Reflection.MemberInfo member, long objectRequired) { }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
public virtual void RegisterObject(object obj, long objectID) { }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
public void RegisterObject(object obj, long objectID, System.Runtime.Serialization.SerializationInfo info) { }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
public void RegisterObject(object obj, long objectID, System.Runtime.Serialization.SerializationInfo? info, long idOfContainingObj, System.Reflection.MemberInfo? member) { }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("ObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
public void RegisterObject(object obj, long objectID, System.Runtime.Serialization.SerializationInfo? info, long idOfContainingObj, System.Reflection.MemberInfo? member, int[]? arrayIndex) { }
}
public abstract partial class SerializationBinder
@@ -135,6 +146,7 @@ public sealed partial class SerializationObjectManager
{
public SerializationObjectManager(System.Runtime.Serialization.StreamingContext context) { }
public void RaiseOnSerializedEvent() { }
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("SerializationObjectManager is not trim compatible because the Type of objects being managed cannot be statically discovered.")]
public void RegisterObject(object obj) { }
}
public partial class SurrogateSelector : System.Runtime.Serialization.ISurrogateSelector
@@ -184,8 +196,10 @@ public BinaryFormatter(System.Runtime.Serialization.ISurrogateSelector? selector
public System.Runtime.Serialization.ISurrogateSelector? SurrogateSelector { get { throw null; } set { } }
public System.Runtime.Serialization.Formatters.FormatterTypeStyle TypeFormat { get { throw null; } set { } }
[System.ObsoleteAttribute("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0011", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("BinaryFormatter serialization is not trim compatible because the Type of objects being processed cannot be statically discovered.")]
public object Deserialize(System.IO.Stream serializationStream) { throw null; }
[System.ObsoleteAttribute("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0011", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
+ [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("BinaryFormatter serialization is not trim compatible because the Type of objects being processed cannot be statically discovered.")]
public void Serialize(System.IO.Stream serializationStream, object graph) { }
}
}
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.Runtime.Serialization.Formatters/src/ILLink/ILLink.Suppressions.xml
deleted file mode 100644
index 9b389862e0c92f..00000000000000
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/ILLink/ILLink.Suppressions.xml
+++ /dev/null
@@ -1,65 +0,0 @@
-
-
-
-
- ILLink
- IL2026
- member
- M:System.Runtime.Serialization.Formatters.Binary.ObjectReader.TopLevelAssemblyTypeResolver.ResolveType(System.Reflection.Assembly,System.String,System.Boolean)
-
-
- ILLink
- IL2026
- member
- M:System.Runtime.Serialization.FormatterServices.GetTypeFromAssembly(System.Reflection.Assembly,System.String)
-
-
- ILLink
- IL2057
- member
- M:System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetSimplyNamedTypeFromAssembly(System.Reflection.Assembly,System.String,System.Type@)
-
-
- ILLink
- IL2067
- member
- M:System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(System.Type)
-
-
- ILLink
- IL2070
- member
- M:System.Runtime.Serialization.FormatterServices.GetSerializableFields(System.Type)
-
-
- ILLink
- IL2070
- member
- M:System.Runtime.Serialization.ObjectManager.GetDeserializationConstructor(System.Type)
-
-
- ILLink
- IL2070
- member
- M:System.Runtime.Serialization.SerializationEvents.GetMethodsWithAttribute(System.Type,System.Type)
-
-
- ILLink
- IL2075
- member
- M:System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(System.Type)
-
-
- ILLink
- IL2075
- member
- M:System.Runtime.Serialization.ObjectManager.DoValueTypeFixup(System.Reflection.FieldInfo,System.Runtime.Serialization.ObjectHolder,System.Object)
-
-
- ILLink
- IL2077
- member
- M:System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObject(System.Runtime.Serialization.Formatters.Binary.ParseRecord)
-
-
-
\ No newline at end of file
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatter.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatter.cs
index 5d97328d8569db..8748e6901547de 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatter.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatter.cs
@@ -1,10 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.IO;
-using System.Globalization;
using System.Collections;
-using System.Reflection;
+using System.Diagnostics.CodeAnalysis;
+using System.Globalization;
+using System.IO;
namespace System.Runtime.Serialization
{
@@ -21,6 +21,7 @@ protected Formatter()
}
[Obsolete(Obsoletions.BinaryFormatterMessage, DiagnosticId = Obsoletions.BinaryFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
+ [RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public abstract object Deserialize(Stream serializationStream);
protected virtual object? GetNext(out long objID)
@@ -61,6 +62,7 @@ protected virtual long Schedule(object? obj)
}
[Obsolete(Obsoletions.BinaryFormatterMessage, DiagnosticId = Obsoletions.BinaryFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
+ [RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public abstract void Serialize(Stream serializationStream, object graph);
protected abstract void WriteArray(object obj, string name, Type memberType);
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/FormatterServices.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/FormatterServices.cs
index 3403898e2e2b37..b3f94a260d7c53 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/FormatterServices.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/FormatterServices.cs
@@ -1,10 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Diagnostics;
-using System.Diagnostics.CodeAnalysis;
using System.Collections.Concurrent;
using System.Collections.Generic;
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters;
@@ -16,7 +16,11 @@ public static class FormatterServices
{
private static readonly ConcurrentDictionary s_memberInfoTable = new ConcurrentDictionary();
- private static FieldInfo[] InternalGetSerializableMembers(Type type)
+ [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
+ Justification = "The Type is annotated with All, which will preserve base type fields.")]
+ private static FieldInfo[] InternalGetSerializableMembers(
+ // currently the only way to preserve base, non-public fields is to use All
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type)
{
Debug.Assert(type != null);
@@ -79,7 +83,8 @@ private static FieldInfo[] InternalGetSerializableMembers(Type type)
return typeMembers;
}
- private static FieldInfo[] GetSerializableFields(Type type)
+ private static FieldInfo[] GetSerializableFields(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] Type type)
{
// Get the list of all fields
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
@@ -156,12 +161,15 @@ private static bool GetParentTypes(Type parentType, out Type[]? parentTypes, out
return unique;
}
- public static MemberInfo[] GetSerializableMembers(Type type)
+ public static MemberInfo[] GetSerializableMembers(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type)
{
return GetSerializableMembers(type, new StreamingContext(StreamingContextStates.All));
}
- public static MemberInfo[] GetSerializableMembers(Type type, StreamingContext context)
+ public static MemberInfo[] GetSerializableMembers(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type,
+ StreamingContext context)
{
if (type == null)
{
@@ -189,7 +197,9 @@ public static object GetUninitializedObject(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type type) => RuntimeHelpers.GetUninitializedObject(type);
- public static object GetSafeUninitializedObject(Type type) => RuntimeHelpers.GetUninitializedObject(type);
+ public static object GetSafeUninitializedObject(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
+ Type type) => RuntimeHelpers.GetUninitializedObject(type);
internal static void SerializationSetValue(MemberInfo fi, object? target, object? value)
{
@@ -294,6 +304,7 @@ public static ISerializationSurrogate GetSurrogateForCyclicalReference(ISerializ
return new SurrogateForCyclicalReference(innerSurrogate);
}
+ [RequiresUnreferencedCode("Types might be removed")]
public static Type? GetTypeFromAssembly(Assembly assem, string name)
{
if (assem == null)
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.Core.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.Core.cs
index 939b78ad9535dc..8d3e437a0b5c5c 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.Core.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.Core.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Diagnostics.CodeAnalysis;
using System.IO;
namespace System.Runtime.Serialization.Formatters.Binary
@@ -8,6 +9,7 @@ namespace System.Runtime.Serialization.Formatters.Binary
public sealed partial class BinaryFormatter : IFormatter
{
[Obsolete(Obsoletions.BinaryFormatterMessage, DiagnosticId = Obsoletions.BinaryFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
+ [RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public object Deserialize(Stream serializationStream)
{
// don't refactor the 'throw' into a helper method; linker will have difficulty trimming
@@ -58,6 +60,7 @@ public object Deserialize(Stream serializationStream)
}
[Obsolete(Obsoletions.BinaryFormatterMessage, DiagnosticId = Obsoletions.BinaryFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
+ [RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public void Serialize(Stream serializationStream, object graph)
{
// don't refactor the 'throw' into a helper method; linker will have difficulty trimming
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.PlatformNotSupported.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.PlatformNotSupported.cs
index 4d56deea06fafb..59970dabe4a6b1 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.PlatformNotSupported.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.PlatformNotSupported.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Diagnostics.CodeAnalysis;
using System.IO;
namespace System.Runtime.Serialization.Formatters.Binary
@@ -8,10 +9,12 @@ namespace System.Runtime.Serialization.Formatters.Binary
public sealed partial class BinaryFormatter : IFormatter
{
[Obsolete(Obsoletions.BinaryFormatterMessage, DiagnosticId = Obsoletions.BinaryFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
+ [RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public object Deserialize(Stream serializationStream)
=> throw new PlatformNotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
[Obsolete(Obsoletions.BinaryFormatterMessage, DiagnosticId = Obsoletions.BinaryFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
+ [RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public void Serialize(Stream serializationStream, object graph)
=> throw new PlatformNotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
}
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectInfo.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectInfo.cs
index 64f4e6ddcd830d..7d9036a907ebfc 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectInfo.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectInfo.cs
@@ -1,10 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Threading;
using System.Collections.Generic;
-using System.Reflection;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
+using System.Reflection;
+using System.Threading;
namespace System.Runtime.Serialization.Formatters.Binary
{
@@ -15,6 +16,7 @@ internal sealed class WriteObjectInfo
{
internal int _objectInfoId;
internal object? _obj;
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
internal Type? _objectType;
internal bool _isSi;
@@ -65,6 +67,7 @@ private void InternalInit()
_binderAssemblyString = null;
}
+ [RequiresUnreferencedCode("It isn't possible to statically get the Type of object")]
internal static WriteObjectInfo Serialize(object obj, ISurrogateSelector? surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder? binder)
{
WriteObjectInfo woi = GetObjectInfo(serObjectInfoInit);
@@ -73,6 +76,7 @@ internal static WriteObjectInfo Serialize(object obj, ISurrogateSelector? surrog
}
// Write constructor
+ [RequiresUnreferencedCode("It isn't possible to statically get the Type of object")]
internal void InitSerialize(object obj, ISurrogateSelector? surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder? binder)
{
_context = context;
@@ -118,7 +122,13 @@ internal void InitSerialize(object obj, ISurrogateSelector? surrogateSelector, S
}
}
- internal static WriteObjectInfo Serialize(Type objectType, ISurrogateSelector? surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder? binder)
+ internal static WriteObjectInfo Serialize(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType,
+ ISurrogateSelector? surrogateSelector,
+ StreamingContext context,
+ SerObjectInfoInit serObjectInfoInit,
+ IFormatterConverter converter,
+ SerializationBinder? binder)
{
WriteObjectInfo woi = GetObjectInfo(serObjectInfoInit);
woi.InitSerialize(objectType, surrogateSelector, context, serObjectInfoInit, converter, binder);
@@ -126,7 +136,13 @@ internal static WriteObjectInfo Serialize(Type objectType, ISurrogateSelector? s
}
// Write Constructor used for array types or null members
- internal void InitSerialize(Type objectType, ISurrogateSelector? surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder? binder)
+ internal void InitSerialize(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType,
+ ISurrogateSelector? surrogateSelector,
+ StreamingContext context,
+ SerObjectInfoInit serObjectInfoInit,
+ IFormatterConverter converter,
+ SerializationBinder? binder)
{
_objectType = objectType;
_context = context;
@@ -313,6 +329,7 @@ internal sealed class ReadObjectInfo
internal int _objectInfoId;
internal static int _readObjectInfoCounter;
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
internal Type? _objectType;
internal ObjectManager? _objectManager;
@@ -347,14 +364,28 @@ internal void PrepareForReuse()
_lastPosition = 0;
}
- internal static ReadObjectInfo Create(Type objectType, ISurrogateSelector? surrogateSelector, StreamingContext context, ObjectManager? objectManager, SerObjectInfoInit? serObjectInfoInit, IFormatterConverter? converter, bool bSimpleAssembly)
+ internal static ReadObjectInfo Create(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType,
+ ISurrogateSelector? surrogateSelector,
+ StreamingContext context,
+ ObjectManager? objectManager,
+ SerObjectInfoInit? serObjectInfoInit,
+ IFormatterConverter? converter,
+ bool bSimpleAssembly)
{
ReadObjectInfo roi = GetObjectInfo(serObjectInfoInit);
roi.Init(objectType, surrogateSelector, context, objectManager, serObjectInfoInit, converter, bSimpleAssembly);
return roi;
}
- internal void Init(Type objectType, ISurrogateSelector? surrogateSelector, StreamingContext context, ObjectManager? objectManager, SerObjectInfoInit? serObjectInfoInit, IFormatterConverter? converter, bool bSimpleAssembly)
+ internal void Init(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType,
+ ISurrogateSelector? surrogateSelector,
+ StreamingContext context,
+ ObjectManager? objectManager,
+ SerObjectInfoInit? serObjectInfoInit,
+ IFormatterConverter? converter,
+ bool bSimpleAssembly)
{
_objectType = objectType;
_objectManager = objectManager;
@@ -366,14 +397,32 @@ internal void Init(Type objectType, ISurrogateSelector? surrogateSelector, Strea
InitReadConstructor(objectType, surrogateSelector, context);
}
- internal static ReadObjectInfo Create(Type? objectType, string[] memberNames, Type[]? memberTypes, ISurrogateSelector? surrogateSelector, StreamingContext context, ObjectManager? objectManager, SerObjectInfoInit? serObjectInfoInit, IFormatterConverter? converter, bool bSimpleAssembly)
+ internal static ReadObjectInfo Create(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? objectType,
+ string[] memberNames,
+ Type[]? memberTypes,
+ ISurrogateSelector? surrogateSelector,
+ StreamingContext context,
+ ObjectManager? objectManager,
+ SerObjectInfoInit? serObjectInfoInit,
+ IFormatterConverter? converter,
+ bool bSimpleAssembly)
{
ReadObjectInfo roi = GetObjectInfo(serObjectInfoInit);
roi.Init(objectType, memberNames, memberTypes, surrogateSelector, context, objectManager, serObjectInfoInit, converter, bSimpleAssembly);
return roi;
}
- internal void Init(Type? objectType, string[] memberNames, Type[]? memberTypes, ISurrogateSelector? surrogateSelector, StreamingContext context, ObjectManager? objectManager, SerObjectInfoInit? serObjectInfoInit, IFormatterConverter? converter, bool bSimpleAssembly)
+ internal void Init(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? objectType,
+ string[] memberNames,
+ Type[]? memberTypes,
+ ISurrogateSelector? surrogateSelector,
+ StreamingContext context,
+ ObjectManager? objectManager,
+ SerObjectInfoInit? serObjectInfoInit,
+ IFormatterConverter? converter,
+ bool bSimpleAssembly)
{
_objectType = objectType;
_objectManager = objectManager;
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectReader.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectReader.cs
index cb1d8f45cd6b4c..ae2f5423c27f23 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectReader.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectReader.cs
@@ -1,16 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Reflection;
-using System.Diagnostics;
-using System.Collections;
namespace System.Runtime.Serialization.Formatters.Binary
{
internal sealed class ObjectReader
{
+ private const string ObjectReaderUnreferencedCodeMessage = "ObjectReader requires unreferenced code";
+
// System.Serializer information
internal Stream _stream;
internal ISurrogateSelector? _surrogates;
@@ -74,6 +76,8 @@ internal ObjectReader(Stream stream, ISurrogateSelector? selector, StreamingCont
_binder = binder;
_formatterEnums = formatterEnums;
}
+
+ [RequiresUnreferencedCode("Types might be removed")]
internal object Deserialize(BinaryParser serParser)
{
if (serParser == null)
@@ -162,16 +166,21 @@ internal object CrossAppDomainArray(int index)
return _crossAppDomainArray[index];
}
- internal ReadObjectInfo CreateReadObjectInfo(Type objectType)
+ internal ReadObjectInfo CreateReadObjectInfo(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType)
{
return ReadObjectInfo.Create(objectType, _surrogates, _context, _objectManager, _serObjectInfoInit, _formatterConverter, _isSimpleAssembly);
}
- internal ReadObjectInfo CreateReadObjectInfo(Type? objectType, string[] memberNames, Type[]? memberTypes)
+ internal ReadObjectInfo CreateReadObjectInfo(
+ [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? objectType,
+ string[] memberNames,
+ Type[]? memberTypes)
{
return ReadObjectInfo.Create(objectType, memberNames, memberTypes, _surrogates, _context, _objectManager, _serObjectInfoInit, _formatterConverter, _isSimpleAssembly);
}
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
internal void Parse(ParseRecord pr)
{
switch (pr._parseTypeEnum)
@@ -218,6 +227,7 @@ private void ParseError(ParseRecord processing, ParseRecord onStack)
private void ParseSerializedStreamHeaderEnd(ParseRecord pr) => _stack!.Pop();
// New object encountered in stream
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseObject(ParseRecord pr)
{
if (!_fullDeserialization)
@@ -300,6 +310,7 @@ private void ParseObject(ParseRecord pr)
}
// End of object encountered in stream
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseObjectEnd(ParseRecord pr)
{
Debug.Assert(_stack != null);
@@ -359,6 +370,7 @@ private void ParseObjectEnd(ParseRecord pr)
}
// Array object encountered in stream
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseArray(ParseRecord pr)
{
Debug.Assert(_stack != null);
@@ -524,6 +536,7 @@ private void NextRectangleMap(ParseRecord pr)
// Array object item encountered in stream
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseArrayMember(ParseRecord pr)
{
Debug.Assert(_stack != null);
@@ -701,6 +714,7 @@ private void ParseArrayMember(ParseRecord pr)
objectPr._memberIndex++;
}
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseArrayMemberEnd(ParseRecord pr)
{
// If this is a nested array object, then pop the stack
@@ -711,6 +725,7 @@ private void ParseArrayMemberEnd(ParseRecord pr)
}
// Object member encountered in stream
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseMember(ParseRecord pr)
{
Debug.Assert(_stack != null);
@@ -825,6 +840,7 @@ private void ParseMember(ParseRecord pr)
}
// Object member end encountered in stream
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseMemberEnd(ParseRecord pr)
{
switch (pr._memberTypeEnum)
@@ -846,6 +862,7 @@ private void ParseMemberEnd(ParseRecord pr)
}
// Processes a string object by getting an internal ID for it and registering it with the objectManager
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void ParseString(ParseRecord pr, ParseRecord parentPr)
{
// Process String class
@@ -857,11 +874,13 @@ private void ParseString(ParseRecord pr, ParseRecord parentPr)
}
}
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void RegisterObject(object obj, ParseRecord pr, ParseRecord? objectPr)
{
RegisterObject(obj, pr, objectPr, false);
}
+ [RequiresUnreferencedCode(ObjectReaderUnreferencedCodeMessage)]
private void RegisterObject(object? obj, ParseRecord pr, ParseRecord? objectPr, bool bIsString)
{
if (!pr._isRegistered)
@@ -937,6 +956,7 @@ internal long GetId(long objectId)
return -1 * objectId;
}
+ [RequiresUnreferencedCode("Types might be removed")]
internal Type? Bind(string assemblyString, string typeString)
{
Type? type = null;
@@ -957,6 +977,7 @@ internal sealed class TypeNAssembly
public string? AssemblyName;
}
+ [RequiresUnreferencedCode("Types might be removed")]
internal Type? FastBindToType(string? assemblyName, string typeName)
{
Type? type = null;
@@ -1046,6 +1067,7 @@ internal sealed class TypeNAssembly
return null;
}
+ [RequiresUnreferencedCode("Types might be removed")]
private static void GetSimplyNamedTypeFromAssembly(Assembly assm, string typeName, ref Type? type)
{
// Catching any exceptions that could be thrown from a failure on assembly load
@@ -1069,6 +1091,7 @@ private static void GetSimplyNamedTypeFromAssembly(Assembly assm, string typeNam
private string? _previousName;
private Type? _previousType;
+ [RequiresUnreferencedCode("Types might be removed")]
internal Type? GetType(BinaryAssemblyInfo assemblyInfo, string name)
{
Type? objectType;
@@ -1122,6 +1145,7 @@ public TopLevelAssemblyTypeResolver(Assembly topLevelAssembly)
_topLevelAssembly = topLevelAssembly;
}
+ [RequiresUnreferencedCode("Types might be removed")]
public Type? ResolveType(Assembly? assembly, string simpleTypeName, bool ignoreCase)
{
if (assembly == null)
diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectWriter.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectWriter.cs
index 7466fd9132760b..cf57a01929bb1b 100644
--- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectWriter.cs
+++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryObjectWriter.cs
@@ -1,14 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
namespace System.Runtime.Serialization.Formatters.Binary
{
internal sealed class ObjectWriter
{
+ private const string ObjectWriterUnreferencedCodeMessage = "ObjectWriter requires unreferenced code";
+
private Queue