Sitelet https://github.com/DapperLib/Dapper/commit/d970cf215e6ed48a14231dcf83fd99362f4c6b9e
Skip to content

Commit d970cf2

Browse files
authored
Suppress trim and AOT diagnostics in Dapper fallback paths (#2233)
1 parent 8baab07 commit d970cf2

5 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎Dapper/DefaultTypeMap.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Reflection;
56

@@ -33,6 +34,7 @@ internal static MethodInfo GetPropertySetterOrThrow(PropertyInfo propertyInfo, T
3334

3435
static MethodInfo Throw(PropertyInfo propertyInfo) => throw new InvalidOperationException("Property setting not found for: " + propertyInfo?.Name);
3536
}
37+
[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "The property was obtained from the mapped type and its setter must be preserved whenever the property is preserved.")]
3638
internal static MethodInfo? GetPropertySetter(PropertyInfo propertyInfo, Type type)
3739
{
3840
if (propertyInfo.DeclaringType == type) return propertyInfo.GetSetMethod(true);
@@ -46,6 +48,7 @@ internal static MethodInfo GetPropertySetterOrThrow(PropertyInfo propertyInfo, T
4648
null)!.GetSetMethod(true);
4749
}
4850

51+
[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Dapper's runtime mapping contract requires the mapped type's properties to be preserved.")]
4952
internal static List<PropertyInfo> GetSettableProps(Type t)
5053
{
5154
return t
@@ -54,6 +57,7 @@ internal static List<PropertyInfo> GetSettableProps(Type t)
5457
.ToList();
5558
}
5659

60+
[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "Dapper's runtime mapping contract requires the mapped type's fields to be preserved.")]
5761
private static FieldInfo[] GetSettableFields(Type t)
5862
{
5963
return t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

‎Dapper/NRT.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#if !NET5_0_OR_GREATER
22
namespace System.Diagnostics.CodeAnalysis
33
{
4+
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
5+
internal sealed class UnconditionalSuppressMessageAttribute : Attribute
6+
{
7+
public UnconditionalSuppressMessageAttribute(string category, string checkId) { }
8+
9+
public string? Justification { get; set; }
10+
}
11+
412
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
513
internal sealed class NotNullWhenAttribute : Attribute
614
{

‎Dapper/SqlDataRecordListTVPParameter.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Data;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
67
using System.Reflection;
78
using System.Reflection.Emit;
@@ -64,6 +65,8 @@ private static Action<IDbDataParameter, string> GetUDT(Type type)
6465
}
6566
}
6667

68+
[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "The provider parameter type is only known at runtime and must preserve its public properties.")]
69+
[UnconditionalSuppressMessage("Aot", "IL3050", Justification = "This fallback is only used for provider-specific structured parameters and is not supported by Dapper's AOT interceptors.")]
6770
static Action<IDbDataParameter, string?> CreateFor(Type type, string nameProperty, int sqlDbType)
6871
{
6972
var name = type.GetProperty(nameProperty, BindingFlags.Public | BindingFlags.Instance);

‎Dapper/SqlMapper.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ public static void AddTypeHandlerImpl(Type type, ITypeHandler? handler, bool clo
365365
AddTypeHandlerCore(type, handler, true); // do not allow suppress clone
366366
}
367367

368+
[UnconditionalSuppressMessage("Aot", "IL3050", Justification = "Runtime type-handler registration requires the caller to statically root the corresponding TypeHandlerCache<T> instantiation.")]
368369
private static void AddTypeHandlerCore(Type type, ITypeHandler? handler, bool clone)
369370
{
370371
if (type is null) throw new ArgumentNullException(nameof(type));
@@ -458,6 +459,7 @@ public static void SetDbType(IDataParameter parameter, object value)
458459
[Obsolete(ObsoleteInternalUsageOnly, false)]
459460
[Browsable(false)]
460461
[EditorBrowsable(EditorBrowsableState.Never)]
462+
[UnconditionalSuppressMessage("Aot", "IL3050", Justification = "Runtime generic construction is limited to the optional SQL data-record handler path, which is not supported by Dapper's AOT interceptors.")]
461463
public static DbType? LookupDbType(Type type, string name, bool demand, out ITypeHandler? handler)
462464
{
463465
handler = null;
@@ -2554,6 +2556,8 @@ private static bool IsValueTuple(Type? type) => (type?.IsValueType == true
25542556
&& type.FullName?.StartsWith("System.ValueTuple`", StringComparison.Ordinal) == true)
25552557
|| (type is not null && IsValueTuple(Nullable.GetUnderlyingType(type)));
25562558

2559+
[UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "The runtime parameter generator requires the parameter object's public properties and constructors to be preserved.")]
2560+
[UnconditionalSuppressMessage("Aot", "IL3050", Justification = "This runtime parameter generator is a fallback for non-intercepted calls and is not used by generated AOT code.")]
25572561
internal static Action<IDbCommand, object?> CreateParamInfoGenerator(Identity identity, bool checkForDuplicates, bool removeUnused, IList<LiteralToken> literals)
25582562
{
25592563
Type type = identity.ParametersType!;

‎Dapper/TypeExtensions.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Reflection;
34

45
namespace Dapper
56
{
67
internal static class TypeExtensions
78
{
9+
[UnconditionalSuppressMessage("Trimming", "IL2070", Justification = "All callers use framework types whose public methods are preserved.")]
810
public static MethodInfo? GetPublicInstanceMethod(this Type type, string name, Type[] types)
911
=> type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public, null, types, null);
1012
}

0 commit comments

Comments
 (0)