Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ internal static class IEnumerableConverterFactoryHelpers
private const string ImmutableSortedDictionaryTypeName = "System.Collections.Immutable.ImmutableSortedDictionary";

private const string CreateRangeMethodName = "CreateRange";
private const string CreateRangeMethodNameForEnumerable = "CreateRange`1";
private const string CreateRangeMethodNameForDictionary = "CreateRange`2";

private const string ImmutableCollectionsAssembly = "System.Collections.Immutable";
// Don't use DynamicDependency attributes to the Immutable Collection types so they can be trimmed in applications that don't use Immutable Collections.
internal const string ImmutableConvertersUnreferencedCodeMessage = "System.Collections.Immutable converters use Reflection to find and create Immutable Collection types, which requires unreferenced code.";

internal static Type? GetCompatibleGenericBaseClass(this Type type, Type baseType)
{
Expand Down Expand Up @@ -151,16 +150,7 @@ public static bool IsImmutableEnumerableType(this Type type)
}
}

[DynamicDependency(CreateRangeMethodNameForEnumerable, ImmutableArrayTypeName, ImmutableCollectionsAssembly)]
[DynamicDependency(CreateRangeMethodNameForEnumerable, ImmutableListTypeName, ImmutableCollectionsAssembly)]
[DynamicDependency(CreateRangeMethodNameForEnumerable, ImmutableStackTypeName, ImmutableCollectionsAssembly)]
[DynamicDependency(CreateRangeMethodNameForEnumerable, ImmutableQueueTypeName, ImmutableCollectionsAssembly)]
[DynamicDependency(CreateRangeMethodNameForEnumerable, ImmutableSortedSetTypeName, ImmutableCollectionsAssembly)]
[DynamicDependency(CreateRangeMethodNameForEnumerable, ImmutableHashSetTypeName, ImmutableCollectionsAssembly)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
Justification = "The CreateRange method is preserved by the DynamicDependency attribute.")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "Immutable collections don't have trimming annotations.")]
[RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)]
public static MethodInfo GetImmutableEnumerableCreateRangeMethod(this Type type, Type elementType)
{
Type? constructingType = GetImmutableEnumerableConstructingType(type);
Expand All @@ -183,12 +173,7 @@ public static MethodInfo GetImmutableEnumerableCreateRangeMethod(this Type type,
return null!;
}

[DynamicDependency(CreateRangeMethodNameForDictionary, ImmutableDictionaryTypeName, ImmutableCollectionsAssembly)]
[DynamicDependency(CreateRangeMethodNameForDictionary, ImmutableSortedDictionaryTypeName, ImmutableCollectionsAssembly)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
Justification = "The CreateRange method is preserved by the DynamicDependency attribute.")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "Immutable collections don't have trimming annotations.")]
[RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)]
public static MethodInfo GetImmutableDictionaryCreateRangeMethod(this Type type, Type keyType, Type valueType)
{
Type? constructingType = GetImmutableDictionaryConstructingType(type);
Expand All @@ -211,9 +196,7 @@ public static MethodInfo GetImmutableDictionaryCreateRangeMethod(this Type type,
return null!;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Json can't take a direct dependency on Collections.Immutable." +
"The types used here will be preserved by the DynamicDependency attributes on the only caller - GetImmutableEnumerableCreateRangeMethod.")]
[RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)]
private static Type? GetImmutableEnumerableConstructingType(Type type)
{
Debug.Assert(type.IsImmutableEnumerableType());
Expand Down Expand Up @@ -257,9 +240,7 @@ public static MethodInfo GetImmutableDictionaryCreateRangeMethod(this Type type,
return underlyingType.Assembly.GetType(constructingTypeName);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Json can't take a direct dependency on Collections.Immutable." +
"The types used here will be preserved by the DynamicDependency attributes on the only caller - GetImmutableDictionaryCreateRangeMethod.")]
[RequiresUnreferencedCode(ImmutableConvertersUnreferencedCodeMessage)]
private static Type? GetImmutableDictionaryConstructingType(Type type)
{
Debug.Assert(type.IsImmutableDictionaryType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization.Metadata;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -11,6 +12,11 @@ internal sealed class ImmutableDictionaryOfTKeyTValueConverter<TCollection, TKey
where TCollection : IReadOnlyDictionary<TKey, TValue>
where TKey : notnull
{
[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public ImmutableDictionaryOfTKeyTValueConverter()
{
}

protected override void Add(TKey key, in TValue value, JsonSerializerOptions options, ref ReadStack state)
{
((Dictionary<TKey, TValue>)state.Current.ReturnValue!)[key] = value;
Expand All @@ -23,6 +29,8 @@ protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStac
state.Current.ReturnValue = new Dictionary<TKey, TValue>();
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "The ctor is marked RequiresUnreferencedCode.")]
protected override void ConvertCollection(ref ReadStack state, JsonSerializerOptions options)
{
JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization.Metadata;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -10,6 +11,11 @@ internal sealed class ImmutableEnumerableOfTConverter<TCollection, TElement>
: IEnumerableDefaultConverter<TCollection, TElement>
where TCollection : IEnumerable<TElement>
{
[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public ImmutableEnumerableOfTConverter()
{
}

protected override void Add(in TElement value, ref ReadStack state)
{
((List<TElement>)state.Current.ReturnValue!).Add(value);
Expand All @@ -22,6 +28,8 @@ protected override void CreateCollection(ref Utf8JsonReader reader, ref ReadStac
state.Current.ReturnValue = new List<TElement>();
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "The ctor is marked RequiresUnreferencedCode.")]
protected override void ConvertCollection(ref ReadStack state, JsonSerializerOptions options)
{
JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public abstract JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, T

public abstract Action<TCollection, object?> CreateAddMethodDelegate<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] TCollection>();

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public abstract Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollection, TElement>();

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public abstract Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeDelegate<TCollection, TKey, TValue>();

public abstract Func<object, TProperty> CreatePropertyGetter<TProperty>(PropertyInfo propertyInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@ private static DynamicMethod CreateAddMethodDelegate(
return dynamicMethod;
}

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public override Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollection, TElement>() =>
CreateDelegate<Func<IEnumerable<TElement>, TCollection>>(
CreateImmutableEnumerableCreateRangeDelegate(typeof(TCollection), typeof(TElement), typeof(IEnumerable<TElement>)));

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
private static DynamicMethod CreateImmutableEnumerableCreateRangeDelegate(Type collectionType, Type elementType, Type enumerableType)
{
MethodInfo realMethod = collectionType.GetImmutableEnumerableCreateRangeMethod(elementType);
Expand All @@ -196,10 +198,12 @@ private static DynamicMethod CreateImmutableEnumerableCreateRangeDelegate(Type c
return dynamicMethod;
}

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public override Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeDelegate<TCollection, TKey, TValue>() =>
CreateDelegate<Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection>>(
CreateImmutableDictionaryCreateRangeDelegate(typeof(TCollection), typeof(TKey), typeof(TValue), typeof(IEnumerable<KeyValuePair<TKey, TValue>>)));

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
private static DynamicMethod CreateImmutableDictionaryCreateRangeDelegate(Type collectionType, Type keyType, Type valueType, Type enumerableType)
{
MethodInfo realMethod = collectionType.GetImmutableDictionaryCreateRangeMethod(keyType, valueType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ public override JsonTypeInfo.ParameterizedConstructorDelegate<T, TArg0, TArg1, T
};
}

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public override Func<IEnumerable<TElement>, TCollection> CreateImmutableEnumerableCreateRangeDelegate<TCollection, TElement>()
{
MethodInfo createRange = typeof(TCollection).GetImmutableEnumerableCreateRangeMethod(typeof(TElement));
return (Func<IEnumerable<TElement>, TCollection>)createRange.CreateDelegate(
typeof(Func<IEnumerable<TElement>, TCollection>));
}

[RequiresUnreferencedCode(IEnumerableConverterFactoryHelpers.ImmutableConvertersUnreferencedCodeMessage)]
public override Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> CreateImmutableDictionaryCreateRangeDelegate<TCollection, TKey, TValue>()
{
MethodInfo createRange = typeof(TCollection).GetImmutableDictionaryCreateRangeMethod(typeof(TKey), typeof(TValue));
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading