Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address remaining warnings from DataContractSerialization #51200

Merged
10 commits merged into from
Apr 19, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,11 @@ internal XmlFormatGetOnlyCollectionReaderDelegate XmlFormatGetOnlyCollectionRead
}
}

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
internal void IncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
{
_helper.IncrementCollectionCount(xmlWriter, obj, context);
}

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
internal IEnumerator GetEnumeratorForCollection(object obj)
{
return _helper.GetEnumeratorForCollection(obj);
Expand Down Expand Up @@ -812,7 +810,6 @@ internal XmlFormatGetOnlyCollectionReaderDelegate? XmlFormatGetOnlyCollectionRea

private static void DummyIncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context) { }

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
internal void IncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
{
if (_incrementCollectionCountDelegate == null)
Expand All @@ -832,13 +829,13 @@ internal void IncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj,
case CollectionKind.GenericCollection:
case CollectionKind.GenericList:
{
var buildIncrementCollectionCountDelegate = BuildIncrementCollectionCountDelegateMethod.MakeGenericMethod(ItemType);
var buildIncrementCollectionCountDelegate = GetBuildIncrementCollectionCountGenericDelegate(ItemType);
_incrementCollectionCountDelegate = (IncrementCollectionCountDelegate)buildIncrementCollectionCountDelegate.Invoke(null, Array.Empty<object>())!;
}
break;
case CollectionKind.GenericDictionary:
{
var buildIncrementCollectionCountDelegate = BuildIncrementCollectionCountDelegateMethod.MakeGenericMethod(Globals.TypeOfKeyValuePair.MakeGenericType(ItemType.GetGenericArguments()));
var buildIncrementCollectionCountDelegate = GetBuildIncrementCollectionCountGenericDelegate(Globals.TypeOfKeyValuePair.MakeGenericType(ItemType.GetGenericArguments()));
_incrementCollectionCountDelegate = (IncrementCollectionCountDelegate)buildIncrementCollectionCountDelegate.Invoke(null, Array.Empty<object>())!;
}
break;
Expand All @@ -852,6 +849,10 @@ internal void IncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj,
_incrementCollectionCountDelegate(xmlWriter, obj, context);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "The call to MakeGenericMethod is safe due to the fact that CollectionDataContractCriticalHelper.BuildIncrementCollectionCountDelegate<T> is not annotated.")]
private MethodInfo GetBuildIncrementCollectionCountGenericDelegate(Type type) => BuildIncrementCollectionCountDelegateMethod.MakeGenericMethod(type);

private static MethodInfo? s_buildIncrementCollectionCountDelegateMethod;

private static MethodInfo BuildIncrementCollectionCountDelegateMethod
Expand Down Expand Up @@ -880,7 +881,6 @@ private static IncrementCollectionCountDelegate BuildIncrementCollectionCountDel

private CreateGenericDictionaryEnumeratorDelegate? _createGenericDictionaryEnumeratorDelegate;

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
internal IEnumerator GetEnumeratorForCollection(object obj)
{
IEnumerator enumerator = ((IEnumerable)obj).GetEnumerator();
Expand All @@ -889,7 +889,7 @@ internal IEnumerator GetEnumeratorForCollection(object obj)
if (_createGenericDictionaryEnumeratorDelegate == null)
{
var keyValueTypes = ItemType.GetGenericArguments();
MethodInfo buildCreateGenericDictionaryEnumerator = BuildCreateGenericDictionaryEnumerato.MakeGenericMethod(keyValueTypes[0], keyValueTypes[1]);
MethodInfo buildCreateGenericDictionaryEnumerator = GetBuildCreateGenericDictionaryEnumeratorGenericMethod(keyValueTypes);
_createGenericDictionaryEnumeratorDelegate = (CreateGenericDictionaryEnumeratorDelegate)buildCreateGenericDictionaryEnumerator.Invoke(null, Array.Empty<object>())!;
}

Expand All @@ -903,6 +903,10 @@ internal IEnumerator GetEnumeratorForCollection(object obj)
return enumerator;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "The call to MakeGenericMethod is safe due to the fact that CollectionDataContractCriticalHelper.BuildCreateGenericDictionaryEnumerator<K,V> is not annotated.")]
private MethodInfo GetBuildCreateGenericDictionaryEnumeratorGenericMethod(Type[] keyValueTypes) => BuildCreateGenericDictionaryEnumerato.MakeGenericMethod(keyValueTypes[0], keyValueTypes[1]);
joperezr marked this conversation as resolved.
Show resolved Hide resolved

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
internal Type GetCollectionElementType()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ private void ReadCollection(CollectionDataContract collectionContract)
if (isArray)
{
Debug.Assert(growingCollection != null);
MethodInfo ensureArraySizeMethod = XmlFormatGeneratorStatics.EnsureArraySizeMethod.MakeGenericMethod(itemType);
MethodInfo ensureArraySizeMethod = MakeGenericMethod(XmlFormatGeneratorStatics.EnsureArraySizeMethod, itemType);
_ilg.Call(null, ensureArraySizeMethod, growingCollection, i);
_ilg.Stloc(growingCollection);
_ilg.StoreArrayElement(growingCollection, i, value);
Expand All @@ -683,7 +683,7 @@ private void ReadCollection(CollectionDataContract collectionContract)
_ilg.EndFor();
if (isArray)
{
MethodInfo trimArraySizeMethod = XmlFormatGeneratorStatics.TrimArraySizeMethod.MakeGenericMethod(itemType);
MethodInfo trimArraySizeMethod = MakeGenericMethod(XmlFormatGeneratorStatics.TrimArraySizeMethod, itemType);
_ilg.Call(null, trimArraySizeMethod, growingCollection, i);
_ilg.Stloc(_objectLocal);
_ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectWithIdMethod, objectId, _objectLocal);
Expand All @@ -702,6 +702,10 @@ private void ReadCollection(CollectionDataContract collectionContract)
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "The call to MakeGenericMethod is safe due to the fact that EnsureArraySizeMethod and TrimArraySizeMethod are not annotated.")]
private MethodInfo MakeGenericMethod(MethodInfo method, Type itemType) => method.MakeGenericMethod(itemType);

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
private void ReadSimpleDictionary(CollectionDataContract collectionContract, Type keyValueType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ private void WriteCollection(CollectionDataContract collectionContract)
break;
case CollectionKind.GenericCollection:
case CollectionKind.GenericList:
incrementCollectionCountMethod = XmlFormatGeneratorStatics.IncrementCollectionCountGenericMethod.MakeGenericMethod(collectionContract.ItemType);
incrementCollectionCountMethod = MakeIncrementCollectionCountGenericMethod(collectionContract.ItemType);
break;
case CollectionKind.GenericDictionary:
incrementCollectionCountMethod = XmlFormatGeneratorStatics.IncrementCollectionCountGenericMethod.MakeGenericMethod(Globals.TypeOfKeyValuePair.MakeGenericType(collectionContract.ItemType.GetGenericArguments()));
incrementCollectionCountMethod = MakeIncrementCollectionCountGenericMethod(Globals.TypeOfKeyValuePair.MakeGenericType(collectionContract.ItemType.GetGenericArguments()));
break;
}
if (incrementCollectionCountMethod != null)
Expand Down Expand Up @@ -490,6 +490,10 @@ private void WriteCollection(CollectionDataContract collectionContract)
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "The call to MakeGenericMethod is safe due to the fact that IncrementCollectionCountGeneric is not annotated.")]
private MethodInfo MakeIncrementCollectionCountGenericMethod(Type itemType) => XmlFormatGeneratorStatics.IncrementCollectionCountGenericMethod.MakeGenericMethod(itemType);

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
private bool TryWritePrimitive(Type type, LocalBuilder? value, MemberInfo? memberInfo, LocalBuilder? arrayItemIndex, LocalBuilder? name, int nameIndex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ internal abstract class ReflectionReader

private static MethodInfo CollectionSetItemDelegateMethod
{
[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
get
{
if (s_getCollectionSetItemDelegateMethod == null)
Expand Down Expand Up @@ -512,7 +511,6 @@ private object ReflectionCreateCollection(CollectionDataContract collectionContr
return ((KeyValue<K, V>)o).Value;
}

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
private CollectionSetItemDelegate GetCollectionSetItemDelegate<T>(CollectionDataContract collectionContract, object resultCollectionObject, bool isReadOnlyCollection)
{
if (isReadOnlyCollection && collectionContract.Kind == CollectionKind.Array)
Expand Down Expand Up @@ -542,8 +540,8 @@ private CollectionSetItemDelegate GetCollectionSetItemDelegate<T>(CollectionData
{
Type keyType = collectionContract.ItemType.GenericTypeArguments[0];
Type valueType = collectionContract.ItemType.GenericTypeArguments[1];
Func<object, object?> objectToKeyValuePairGetKey = s_objectToKeyValuePairGetKey.MakeGenericMethod(keyType, valueType).CreateDelegate<Func<object, object?>>();
Func<object, object?> objectToKeyValuePairGetValue = s_objectToKeyValuePairGetValue.MakeGenericMethod(keyType, valueType).CreateDelegate<Func<object, object?>>();
Func<object, object?> objectToKeyValuePairGetKey = MakeGenericMethod(s_objectToKeyValuePairGetKey, keyType, valueType).CreateDelegate<Func<object, object?>>();
Func<object, object?> objectToKeyValuePairGetValue = MakeGenericMethod(s_objectToKeyValuePairGetValue, keyType, valueType).CreateDelegate<Func<object, object?>>();

if (collectionContract.Kind == CollectionKind.GenericDictionary)
{
Expand Down Expand Up @@ -607,6 +605,10 @@ private CollectionSetItemDelegate GetCollectionSetItemDelegate<T>(CollectionData
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "The call to MakeGenericMethod is safe due to the fact that ObjectToKeyValuePairGetKey and ObjectToKeyValuePairGetValue are not annotated.")]
private MethodInfo MakeGenericMethod(MethodInfo method, Type keyType, Type valueType) => method.MakeGenericMethod(keyType, valueType);
joperezr marked this conversation as resolved.
Show resolved Hide resolved

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
private bool ReflectionTryReadPrimitiveArray(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, Type type, Type itemType, int arraySize, [NotNullWhen(true)] out object? resultArray)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,8 @@ internal static MethodInfo GetDefaultValueMethod
}
}

[RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod",
Justification = "The call to MakeGenericMethod is safe due to the fact that XmlObjectSerializerWriteContext.GetDefaultValue is not annotated.")]
internal static object? GetDefaultValue(Type type)
{
return GetDefaultValueMethod.MakeGenericMethod(type).Invoke(null, Array.Empty<object>());
Expand Down