diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index 3db4c219dfb241..0c12293f8e4152 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -194,9 +194,9 @@ https://github.com/dotnet/runtime acbbb505492244b4c07a4a368257ba86a1fc02e1 - + https://github.com/mono/linker - 0c4902a114192fce1e7570d998e70d24669e9cc3 + c0ed4ef448496b0dc7db3aeb27bbb194fcdccb58 https://github.com/dotnet/xharness diff --git a/eng/Versions.props b/eng/Versions.props index 261c39bba7ca4e..0d4b05a833ea59 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -155,7 +155,7 @@ 5.0.0-preview-20201009.2 - 6.0.100-preview.2.21126.1 + 6.0.100-preview.2.21167.1 6.0.0-preview.3.21151.1 diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index ed010eff3ce5aa..cb91cbdfdb4efb 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -227,6 +227,7 @@ + @@ -254,6 +255,9 @@ + + + diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverterFactory.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverterFactory.cs index cc67a34eb392e1..24d7c64f80356a 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverterFactory.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverterFactory.cs @@ -1,7 +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.Reflection; +using System.Diagnostics.CodeAnalysis; namespace System.Text.Json.Serialization.Converters { @@ -16,16 +16,27 @@ public override bool CanConvert(Type type) return type.IsEnum; } - public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) + public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) => + Create(type, EnumConverterOptions.AllowNumbers, options); + + internal static JsonConverter Create(Type enumType, EnumConverterOptions converterOptions, JsonSerializerOptions serializerOptions) { - JsonConverter converter = (JsonConverter)Activator.CreateInstance( - typeof(EnumConverter<>).MakeGenericType(type), - BindingFlags.Instance | BindingFlags.Public, - binder: null, - new object[] { EnumConverterOptions.AllowNumbers, options }, - culture: null)!; + return (JsonConverter)Activator.CreateInstance( + GetEnumConverterType(enumType), + new object[] { converterOptions, serializerOptions })!; + } - return converter; + internal static JsonConverter Create(Type enumType, EnumConverterOptions converterOptions, JsonNamingPolicy? namingPolicy, JsonSerializerOptions serializerOptions) + { + return (JsonConverter)Activator.CreateInstance( + GetEnumConverterType(enumType), + new object?[] { converterOptions, namingPolicy, serializerOptions })!; } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:MakeGenericType", + Justification = "'EnumConverter where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because enumType's constructors are not annotated. " + + "But EnumConverter doesn't call new T(), so this is safe.")] + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] + private static Type GetEnumConverterType(Type enumType) => typeof(EnumConverter<>).MakeGenericType(enumType); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverterFactory.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverterFactory.cs index dfab566a636a8e..f42ed1fbcc8c10 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverterFactory.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/NullableConverterFactory.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace System.Text.Json.Serialization.Converters @@ -34,11 +35,17 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer public static JsonConverter CreateValueConverter(Type valueTypeToConvert, JsonConverter valueConverter) { return (JsonConverter)Activator.CreateInstance( - typeof(NullableConverter<>).MakeGenericType(valueTypeToConvert), + GetNullableConverterType(valueTypeToConvert), BindingFlags.Instance | BindingFlags.Public, binder: null, args: new object[] { valueConverter }, culture: null)!; } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:MakeGenericType", + Justification = "'NullableConverter where T : struct' implies 'T : new()', so the trimmer is warning calling MakeGenericType here because valueTypeToConvert's constructors are not annotated. " + + "But NullableConverter doesn't call new T(), so this is safe.")] + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] + private static Type GetNullableConverterType(Type valueTypeToConvert) => typeof(NullableConverter<>).MakeGenericType(valueTypeToConvert); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs index cbe8e8f72d1899..e442176093ded0 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs @@ -96,12 +96,7 @@ internal JsonConverter GetDictionaryKeyConverter(Type keyType) // Use factory pattern to generate an EnumConverter with AllowStrings and AllowNumbers options for dictionary keys. // There will be one converter created for each enum type. JsonConverter GetEnumConverter() - => (JsonConverter)Activator.CreateInstance( - typeof(EnumConverter<>).MakeGenericType(keyType), - BindingFlags.Instance | BindingFlags.Public, - binder: null, - new object[] { EnumConverterOptions.AllowStrings | EnumConverterOptions.AllowNumbers, this }, - culture: null)!; + => EnumConverterFactory.Create(keyType, EnumConverterOptions.AllowStrings | EnumConverterOptions.AllowNumbers, this); } private ConcurrentDictionary? _dictionaryKeyConverters; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs index 65ac02718fb536..ac6fe18b9dbda9 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs @@ -1,7 +1,6 @@ // 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.Text.Json.Serialization.Converters; namespace System.Text.Json.Serialization @@ -52,16 +51,7 @@ public override bool CanConvert(Type typeToConvert) } /// - public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) - { - JsonConverter converter = (JsonConverter)Activator.CreateInstance( - typeof(EnumConverter<>).MakeGenericType(typeToConvert), - BindingFlags.Instance | BindingFlags.Public, - binder: null, - new object?[] { _converterOptions, _namingPolicy, options }, - culture: null)!; - - return converter; - } + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => + EnumConverterFactory.Create(typeToConvert, _converterOptions, _namingPolicy, options); } } diff --git a/src/libraries/tests.proj b/src/libraries/tests.proj index a46f287acb10c1..79c14f9bb52dec 100644 --- a/src/libraries/tests.proj +++ b/src/libraries/tests.proj @@ -292,7 +292,9 @@ +