Skip to content

Commit a9849a1

Browse files
committed
C# 12
1 parent 6bb05d5 commit a9849a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+378
-611
lines changed

src/AutoMapper/ApiCompatBaseline.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Compat issues with assembly AutoMapper:
22
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.AutoMapAttribute' changed from '[AttributeUsageAttribute(1036, AllowMultiple=true)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, AllowMultiple=true)]' in the implementation.
3+
CannotSealType : Type 'AutoMapper.AutoMapperConfigurationException.TypeMapConfigErrors' is actually (has the sealed modifier) sealed in the implementation but not sealed in the contract.
4+
TypeCannotChangeClassification : Type 'AutoMapper.AutoMapperConfigurationException.TypeMapConfigErrors' is a 'struct' in the implementation but is a 'class' in the contract.
5+
CannotSealType : Type 'AutoMapper.DuplicateTypeMapConfigurationException.TypeMapConfigErrors' is actually (has the sealed modifier) sealed in the implementation but not sealed in the contract.
6+
TypeCannotChangeClassification : Type 'AutoMapper.DuplicateTypeMapConfigurationException.TypeMapConfigErrors' is a 'struct' in the implementation but is a 'class' in the contract.
37
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableContextAttribute' exists on 'AutoMapper.IMapper' in the contract but not the implementation.
48
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableContextAttribute' exists on 'AutoMapper.IMapper.Map(System.Object, System.Object, System.Type, System.Type, System.Action<AutoMapper.IMappingOperationOptions<System.Object, System.Object>>)' in the contract but not the implementation.
59
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on parameter 'opts' on member 'AutoMapper.IMapper.Map(System.Object, System.Object, System.Type, System.Type, System.Action<AutoMapper.IMappingOperationOptions<System.Object, System.Object>>)' in the contract but not the implementation.
@@ -24,6 +28,9 @@ CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttri
2428
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TDestination' on member 'AutoMapper.ResolutionContext.AutoMapper.IMapperBase.Map<TSource, TDestination>(TSource)' in the contract but not the implementation.
2529
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TSource' on member 'AutoMapper.ResolutionContext.AutoMapper.IMapperBase.Map<TSource, TDestination>(TSource, TDestination)' in the contract but not the implementation.
2630
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TDestination' on member 'AutoMapper.ResolutionContext.AutoMapper.IMapperBase.Map<TSource, TDestination>(TSource, TDestination)' in the contract but not the implementation.
31+
MembersMustExist : Member 'public void AutoMapper.Configuration.MappingExpression..ctor(AutoMapper.Internal.TypePair, AutoMapper.MemberList)' does not exist in the implementation but it does exist in the contract.
32+
MembersMustExist : Member 'public void AutoMapper.Configuration.MappingExpression<TSource, TDestination>..ctor(AutoMapper.MemberList, System.Type, System.Type)' does not exist in the implementation but it does exist in the contract.
33+
MembersMustExist : Member 'protected void AutoMapper.Configuration.MappingExpressionBase<TSource, TDestination, TMappingExpression>..ctor(AutoMapper.MemberList, System.Type, System.Type)' does not exist in the implementation but it does exist in the contract.
2734
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.IgnoreAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
2835
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.MapAtRuntimeAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
2936
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.MappingOrderAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
@@ -43,4 +50,4 @@ CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttri
4350
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableContextAttribute' exists on 'AutoMapper.QueryableExtensions.Extensions.ProjectTo<TDestination>(System.Linq.IQueryable, AutoMapper.IConfigurationProvider, System.Object, System.Linq.Expressions.Expression<System.Func<TDestination, System.Object>>[])' in the contract but not the implementation.
4451
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on parameter 'parameters' on member 'AutoMapper.QueryableExtensions.Extensions.ProjectTo<TDestination>(System.Linq.IQueryable, AutoMapper.IConfigurationProvider, System.Object, System.Linq.Expressions.Expression<System.Func<TDestination, System.Object>>[])' in the contract but not the implementation.
4552
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TDestination' on member 'AutoMapper.QueryableExtensions.Extensions.ProjectTo<TDestination>(System.Linq.IQueryable, AutoMapper.IConfigurationProvider, System.Object, System.Linq.Expressions.Expression<System.Func<TDestination, System.Object>>[])' in the contract but not the implementation.
46-
Total Issues: 44
53+
Total Issues: 51

src/AutoMapper/AutoMapperMappingException.cs

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,63 +67,37 @@ public override string StackTrace
6767
{
6868
return string.Join(Environment.NewLine,
6969
base.StackTrace
70-
.Split(new[] {Environment.NewLine}, StringSplitOptions.None)
70+
.Split([Environment.NewLine], StringSplitOptions.None)
7171
.Where(str => !str.TrimStart().StartsWith("at AutoMapper.")));
7272
}
7373
}
7474
#endif
7575
}
76-
public class DuplicateTypeMapConfigurationException : Exception
76+
public class DuplicateTypeMapConfigurationException(DuplicateTypeMapConfigurationException.TypeMapConfigErrors[] errors) : Exception
7777
{
78-
public TypeMapConfigErrors[] Errors { get; }
79-
80-
public DuplicateTypeMapConfigurationException(TypeMapConfigErrors[] errors)
78+
public TypeMapConfigErrors[] Errors { get; } = errors;
79+
public override string Message { get; } = GetErrors(errors);
80+
static string GetErrors(TypeMapConfigErrors[] errors)
8181
{
82-
Errors = errors;
83-
var builder = new StringBuilder();
82+
StringBuilder builder = new();
8483
builder.AppendLine("Duplicate CreateMap calls:");
85-
foreach (var error in Errors)
84+
foreach(var error in errors)
8685
{
8786
builder.AppendLine($"{error.Types.SourceType.FullName} to {error.Types.DestinationType.FullName} defined in profiles:");
8887
builder.AppendLine(string.Join(Environment.NewLine, error.ProfileNames));
8988
}
9089
builder.AppendLine("This can cause configuration collisions and inconsistent mappings. Use a single CreateMap call per type pair.");
91-
Message = builder.ToString();
92-
}
93-
94-
public class TypeMapConfigErrors
95-
{
96-
public string[] ProfileNames { get; }
97-
public TypePair Types { get; }
98-
99-
public TypeMapConfigErrors(TypePair types, string[] profileNames)
100-
{
101-
Types = types;
102-
ProfileNames = profileNames;
103-
}
90+
return builder.ToString();
10491
}
105-
106-
public override string Message { get; }
92+
public readonly record struct TypeMapConfigErrors(TypePair Types, string[] ProfileNames);
10793
}
10894
public class AutoMapperConfigurationException : Exception
10995
{
11096
public TypeMapConfigErrors[] Errors { get; }
11197
public TypePair? Types { get; }
11298
public MemberMap MemberMap { get; set; }
11399

114-
public class TypeMapConfigErrors
115-
{
116-
public TypeMap TypeMap { get; }
117-
public string[] UnmappedPropertyNames { get; }
118-
public bool CanConstruct { get; }
119-
120-
public TypeMapConfigErrors(TypeMap typeMap, string[] unmappedPropertyNames, bool canConstruct)
121-
{
122-
TypeMap = typeMap;
123-
UnmappedPropertyNames = unmappedPropertyNames;
124-
CanConstruct = canConstruct;
125-
}
126-
}
100+
public readonly record struct TypeMapConfigErrors(TypeMap TypeMap, string[] UnmappedPropertyNames, bool CanConstruct);
127101

128102
public AutoMapperConfigurationException(string message)
129103
: base(message)
@@ -170,16 +144,15 @@ public override string Message
170144
}
171145
if (Errors != null)
172146
{
173-
var message =
174-
new StringBuilder(
147+
StringBuilder message = new(
175148
"\nUnmapped members were found. Review the types and members below.\nAdd a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type\nFor no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters\n");
176149

177150
foreach (var error in Errors)
178151
{
179152
var len = error.TypeMap.SourceType.FullName.Length +
180153
error.TypeMap.DestinationType.FullName.Length + 5;
181154

182-
message.AppendLine(new string('=', len));
155+
message.AppendLine(new('=', len));
183156
message.AppendLine(error.TypeMap.SourceType.Name + " -> " + error.TypeMap.DestinationType.Name +
184157
" (" +
185158
error.TypeMap.ConfiguredMemberList + " member list)");
@@ -214,7 +187,7 @@ public override string StackTrace
214187
if (Errors != null)
215188
return string.Join(Environment.NewLine,
216189
base.StackTrace
217-
.Split(new[] { Environment.NewLine }, StringSplitOptions.None)
190+
.Split([Environment.NewLine], StringSplitOptions.None)
218191
.Where(str => !str.TrimStart().StartsWith("at AutoMapper."))
219192
.ToArray());
220193

src/AutoMapper/Configuration/Annotations/AutoMapAttribute.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
/// Discovered during scanning assembly scanning for configuration when calling <see cref="O:AutoMapper.IMapperConfigurationExpression.AddMaps"/>
66
/// </summary>
77
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, AllowMultiple = true)]
8-
public sealed class AutoMapAttribute : Attribute
8+
public sealed class AutoMapAttribute(Type sourceType) : Attribute
99
{
10-
public AutoMapAttribute(Type sourceType)
11-
=> SourceType = sourceType;
12-
13-
public Type SourceType { get; }
10+
public Type SourceType { get; } = sourceType;
1411
public bool ReverseMap { get; set; }
1512

1613
/// <summary>

src/AutoMapper/Configuration/Annotations/MappingOrderAttribute.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77
/// Must be used in combination with <see cref="AutoMapAttribute" />
88
/// </remarks>
99
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
10-
public sealed class MappingOrderAttribute : Attribute, IMemberConfigurationProvider
10+
public sealed class MappingOrderAttribute(int value) : Attribute, IMemberConfigurationProvider
1111
{
12-
public int Value { get; }
13-
14-
public MappingOrderAttribute(int value)
15-
{
16-
Value = value;
17-
}
12+
public int Value { get; } = value;
1813

1914
public void ApplyConfiguration(IMemberConfigurationExpression memberConfigurationExpression)
2015
{

src/AutoMapper/Configuration/Annotations/NullSubstituteAttribute.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77
/// Must be used in combination with <see cref="AutoMapAttribute" />
88
/// </remarks>
99
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
10-
public sealed class NullSubstituteAttribute : Attribute, IMemberConfigurationProvider
10+
public sealed class NullSubstituteAttribute(object value) : Attribute, IMemberConfigurationProvider
1111
{
1212
/// <summary>
1313
/// Value to use if source value is null
1414
/// </summary>
15-
public object Value { get; }
16-
17-
public NullSubstituteAttribute(object value)
18-
{
19-
Value = value;
20-
}
15+
public object Value { get; } = value;
2116

2217
public void ApplyConfiguration(IMemberConfigurationExpression memberConfigurationExpression)
2318
{

src/AutoMapper/Configuration/Annotations/SourceMemberAttribute.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
/// Must be used in combination with <see cref="AutoMapAttribute" />
88
/// </remarks>
99
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
10-
public sealed class SourceMemberAttribute : Attribute, IMemberConfigurationProvider
10+
public sealed class SourceMemberAttribute(string name) : Attribute, IMemberConfigurationProvider
1111
{
12-
public string Name { get; }
13-
14-
public SourceMemberAttribute(string name) => Name = name;
12+
public string Name { get; } = name;
1513

1614
public void ApplyConfiguration(IMemberConfigurationExpression memberConfigurationExpression)
1715
{

src/AutoMapper/Configuration/Annotations/ValueConverterAttribute.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
/// Must be used in combination with <see cref="AutoMapAttribute" />
99
/// </remarks>
1010
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
11-
public sealed class ValueConverterAttribute : Attribute, IMemberConfigurationProvider
11+
public sealed class ValueConverterAttribute(Type type) : Attribute, IMemberConfigurationProvider
1212
{
1313
/// <summary>
1414
/// <see cref="IValueConverter{TSourceMember,TDestinationMember}" /> type
1515
/// </summary>
16-
public Type Type { get; }
17-
18-
public ValueConverterAttribute(Type type)
19-
{
20-
Type = type;
21-
}
16+
public Type Type { get; } = type;
2217

2318
public void ApplyConfiguration(IMemberConfigurationExpression memberConfigurationExpression)
2419
{

src/AutoMapper/Configuration/Annotations/ValueResolverAttribute.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
/// Must be used in combination with <see cref="AutoMapAttribute" />
99
/// </remarks>
1010
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
11-
public sealed class ValueResolverAttribute : Attribute, IMemberConfigurationProvider
11+
public sealed class ValueResolverAttribute(Type type) : Attribute, IMemberConfigurationProvider
1212
{
1313
/// <summary>
1414
/// <see cref="IValueResolver{TSource,TDestination,TDestMember}" /> or <see cref="IMemberValueResolver{TSource,TDestination,TSourceMember,TDestMember}" /> type
1515
/// </summary>
16-
public Type Type { get; }
17-
18-
public ValueResolverAttribute(Type type)
19-
{
20-
Type = type;
21-
}
16+
public Type Type { get; } = type;
2217

2318
public void ApplyConfiguration(IMemberConfigurationExpression memberConfigurationExpression)
2419
{

src/AutoMapper/Configuration/ConfigurationValidator.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using AutoMapper.Internal.Mappers;
22
namespace AutoMapper.Configuration;
3-
43
[EditorBrowsable(EditorBrowsableState.Never)]
54
public readonly record struct ConfigurationValidator(IGlobalConfigurationExpression Expression)
65
{
@@ -11,7 +10,7 @@ private void Validate(ValidationContext context)
1110
validator(context);
1211
}
1312
}
14-
public void AssertConfigurationExpressionIsValid(IGlobalConfiguration config, IEnumerable<TypeMap> typeMaps)
13+
public void AssertConfigurationExpressionIsValid(IGlobalConfiguration config, TypeMap[] typeMaps)
1514
{
1615
var duplicateTypeMapConfigs = Expression.Profiles.Append((Profile)Expression)
1716
.SelectMany(p => p.TypeMapConfigs, (profile, typeMap) => (profile, typeMap))
@@ -26,25 +25,23 @@ public void AssertConfigurationExpressionIsValid(IGlobalConfiguration config, IE
2625
}
2726
AssertConfigurationIsValid(config, typeMaps);
2827
}
29-
public void AssertConfigurationIsValid(IGlobalConfiguration config, IEnumerable<TypeMap> typeMaps)
28+
public void AssertConfigurationIsValid(IGlobalConfiguration config, TypeMap[] typeMaps)
3029
{
31-
var maps = typeMaps as TypeMap[] ?? typeMaps.ToArray();
3230
var badTypeMaps =
33-
(from typeMap in maps
31+
(from typeMap in typeMaps
3432
where typeMap.ShouldCheckForValid
3533
let unmappedPropertyNames = typeMap.GetUnmappedPropertyNames()
3634
let canConstruct = typeMap.PassesCtorValidation
3735
where unmappedPropertyNames.Length > 0 || !canConstruct
3836
select new AutoMapperConfigurationException.TypeMapConfigErrors(typeMap, unmappedPropertyNames, canConstruct)
3937
).ToArray();
40-
41-
if (badTypeMaps.Any())
38+
if (badTypeMaps.Length > 0)
4239
{
4340
throw new AutoMapperConfigurationException(badTypeMaps);
4441
}
45-
var typeMapsChecked = new HashSet<TypeMap>();
46-
var configExceptions = new List<Exception>();
47-
foreach (var typeMap in maps)
42+
HashSet<TypeMap> typeMapsChecked = [];
43+
List<Exception> configExceptions = [];
44+
foreach (var typeMap in typeMaps)
4845
{
4946
try
5047
{
@@ -81,8 +78,7 @@ private void DryRunTypeMap(IGlobalConfiguration config, HashSet<TypeMap> typeMap
8178
return;
8279
}
8380
typeMapsChecked.Add(typeMap);
84-
var context = new ValidationContext(types, memberMap, typeMap);
85-
Validate(context);
81+
Validate(new(types, memberMap, typeMap));
8682
if(!typeMap.ShouldCheckForValid)
8783
{
8884
return;
@@ -96,8 +92,7 @@ private void DryRunTypeMap(IGlobalConfiguration config, HashSet<TypeMap> typeMap
9692
{
9793
throw new AutoMapperConfigurationException(memberMap.TypeMap.Types) { MemberMap = memberMap };
9894
}
99-
var context = new ValidationContext(types, memberMap, ObjectMapper: mapperToUse);
100-
Validate(context);
95+
Validate(new(types, memberMap, ObjectMapper: mapperToUse));
10196
if (mapperToUse.GetAssociatedTypes(types) is TypePair newTypes && newTypes != types)
10297
{
10398
DryRunTypeMap(config, typeMapsChecked, newTypes, null, memberMap);

0 commit comments

Comments
 (0)