1- using System ;
1+ using AutoMapper . Internal ;
2+ using AutoMapper . Internal . Mappers ;
3+ using System ;
24using System . Collections . Generic ;
5+ using System . Diagnostics . CodeAnalysis ;
36using System . Linq ;
47using System . Linq . Expressions ;
58using System . Reflection ;
6- using AutoMapper . Extensions . ExpressionMapping ;
7- using AutoMapper . Internal ;
8- using AutoMapper . Internal . Mappers ;
99using static System . Linq . Expressions . Expression ;
1010
11- namespace AutoMapper . Mappers
11+ namespace AutoMapper . Extensions . ExpressionMapping
1212{
1313 public class ExpressionMapper : IObjectMapper
1414 {
@@ -23,7 +23,7 @@ public bool IsMatch(TypePair context) => typeof(LambdaExpression).IsAssignableFr
2323 && typeof ( LambdaExpression ) . IsAssignableFrom ( context . DestinationType )
2424 && context . DestinationType != typeof ( LambdaExpression ) ;
2525
26- public Expression MapExpression ( IGlobalConfiguration configurationProvider , ProfileMap profileMap , MemberMap memberMap , Expression sourceExpression , Expression destExpression )
26+ public Expression MapExpression ( IGlobalConfiguration configurationProvider , ProfileMap profileMap , MemberMap memberMap , Expression sourceExpression , Expression destExpression )
2727 => Call
2828 (
2929 null ,
@@ -37,9 +37,10 @@ public Expression MapExpression(IGlobalConfiguration configurationProvider, Prof
3737 return null ;
3838 }
3939
40+ [ ExcludeFromCodeCoverage ]
4041 internal class MappingVisitor : ExpressionVisitor
4142 {
42- private IList < Type > _destSubTypes = new Type [ 0 ] ;
43+ private IList < Type > _destSubTypes = [ ] ;
4344
4445 private readonly IConfigurationProvider _configurationProvider ;
4546 private readonly TypeMap _typeMap ;
@@ -81,7 +82,7 @@ private MethodCallExpression GetConvertedMethodCall(MethodCallExpression node)
8182 return Call ( convertedMethodCall , convertedArguments ) ;
8283 }
8384
84- private static Type GetConvertingTypeIfExists ( IList < Expression > args , Type t , IList < Expression > arguments )
85+ private static Type GetConvertingTypeIfExists ( System . Collections . ObjectModel . ReadOnlyCollection < Expression > args , Type t , IList < Expression > arguments )
8586 {
8687 var matchingArgument = args . Where ( a => ! a . Type . IsGenericType ( ) ) . FirstOrDefault ( a => a . Type == t ) ;
8788 if ( matchingArgument != null )
@@ -109,7 +110,7 @@ protected override Expression VisitBinary(BinaryExpression node)
109110 CheckNullableToNonNullableChanges ( node . Left , node . Right , ref newLeft , ref newRight ) ;
110111 CheckNullableToNonNullableChanges ( node . Right , node . Left , ref newRight , ref newLeft ) ;
111112 return MakeBinary ( node . NodeType , newLeft , newRight ) ;
112- bool IsNullConstant ( Expression expression ) => expression is ConstantExpression constant && constant . Value == null ;
113+ static bool IsNullConstant ( Expression expression ) => expression is ConstantExpression constant && constant . Value == null ;
113114 }
114115
115116 private static void CheckNullableToNonNullableChanges ( Expression left , Expression right , ref Expression newLeft , ref Expression newRight )
@@ -145,30 +146,30 @@ private static void UpdateToNonNullableExpression(Expression right, out Expressi
145146 : right . Type ;
146147 newRight = Constant ( expression . Value , t ) ;
147148 }
148- else if ( right is UnaryExpression )
149- newRight = ( ( UnaryExpression ) right ) . Operand ;
149+ else if ( right is UnaryExpression unaryExpression )
150+ newRight = unaryExpression . Operand ;
150151 else
151152 throw new AutoMapperMappingException (
152153 "Mapping a BinaryExpression where one side is nullable and the other isn't" ) ;
153154 }
154155
155- private static bool GoingFromNonNullableToNullable ( Expression node , Expression newLeft )
156+ private static bool GoingFromNonNullableToNullable ( Expression node , Expression newLeft )
156157 => ! node . Type . IsNullableType ( ) && newLeft . Type . IsNullableType ( ) ;
157158
158- private static bool BothAreNullable ( Expression node , Expression newLeft )
159+ private static bool BothAreNullable ( Expression node , Expression newLeft )
159160 => node . Type . IsNullableType ( ) && newLeft . Type . IsNullableType ( ) ;
160161
161- private static bool BothAreNonNullable ( Expression node , Expression newLeft )
162+ private static bool BothAreNonNullable ( Expression node , Expression newLeft )
162163 => ! node . Type . IsNullableType ( ) && ! newLeft . Type . IsNullableType ( ) ;
163164
164165 protected override Expression VisitLambda < T > ( Expression < T > node )
165166 {
166- return node . Parameters . Any ( b => b . Type == _oldParam . Type )
167- ? VisitLambdaExpression ( node )
167+ return node . Parameters . Any ( b => b . Type == _oldParam . Type )
168+ ? VisitLambdaExpression ( node )
168169 : VisitAllParametersExpression ( node ) ;
169170 }
170171
171- private Expression VisitLambdaExpression < T > ( Expression < T > expression )
172+ private LambdaExpression VisitLambdaExpression < T > ( Expression < T > expression )
172173 {
173174 var convertedBody = Visit ( expression . Body ) ;
174175 var convertedArguments = expression . Parameters . Select ( e => Visit ( e ) as ParameterExpression ) . ToList ( ) ;
@@ -226,6 +227,7 @@ protected override Expression VisitMember(MemberExpression node)
226227 . Aggregate ( replacedExpression , getExpression ) ;
227228 }
228229
230+ [ ExcludeFromCodeCoverage ]
229231 private class IsConstantExpressionVisitor : ExpressionVisitor
230232 {
231233 public bool IsConstant { get ; private set ; }
@@ -251,22 +253,22 @@ private Expression GetConvertedSubMemberCall(MemberExpression node)
251253 var typeMap = _configurationProvider . Internal ( ) . ResolveTypeMap ( sourceType , destType ) ;
252254 var subVisitor = new MappingVisitor ( _configurationProvider , typeMap , node . Expression , baseExpression , this ) ;
253255 var newExpression = subVisitor . Visit ( node ) ;
254- _destSubTypes = _destSubTypes . Concat ( subVisitor . _destSubTypes ) . ToArray ( ) ;
256+ _destSubTypes = [ .. _destSubTypes , .. subVisitor . _destSubTypes ] ;
255257 return newExpression ;
256258 }
257259
258- private Type GetSourceType ( PropertyMap propertyMap ) =>
260+ private static Type GetSourceType ( PropertyMap propertyMap ) =>
259261 propertyMap . SourceType ??
260262 throw new AutoMapperMappingException (
261- "Could not determine source property type. Make sure the property is mapped." ,
262- null ,
263+ "Could not determine source property type. Make sure the property is mapped." ,
264+ null ,
263265 propertyMap ) ;
264266
265267 private PropertyMap FindPropertyMapOfExpression ( MemberExpression expression )
266268 {
267269 var propertyMap = PropertyMap ( expression ) ;
268- return propertyMap == null && expression . Expression is MemberExpression
269- ? FindPropertyMapOfExpression ( ( MemberExpression ) expression . Expression )
270+ return propertyMap == null && expression . Expression is MemberExpression memberExpression
271+ ? FindPropertyMapOfExpression ( memberExpression )
270272 : propertyMap ;
271273 }
272274
@@ -277,9 +279,9 @@ private PropertyMap PropertyMap(MemberExpression node)
277279 ? null
278280 : ( ! node . Member . DeclaringType . IsAssignableFrom ( _typeMap . DestinationType )
279281 ? null
280- : GetExistingPropertyMapFor ( node . Member , _typeMap ) ) ) ;
282+ : MappingVisitor . GetExistingPropertyMapFor ( node . Member , _typeMap ) ) ) ;
281283
282- private PropertyMap GetExistingPropertyMapFor ( MemberInfo destinationProperty , TypeMap typeMap )
284+ private static PropertyMap GetExistingPropertyMapFor ( MemberInfo destinationProperty , TypeMap typeMap )
283285 {
284286 if ( ! destinationProperty . DeclaringType . IsAssignableFrom ( typeMap . DestinationType ) )
285287 return null ;
@@ -290,7 +292,7 @@ private PropertyMap GetExistingPropertyMapFor(MemberInfo destinationProperty, Ty
290292 private void SetSourceSubTypes ( PropertyMap propertyMap )
291293 {
292294 if ( propertyMap . SourceMember is PropertyInfo info )
293- _destSubTypes = info . PropertyType . GetTypeInfo ( ) . GenericTypeArguments . Concat ( new [ ] { info . PropertyType } ) . ToList ( ) ;
295+ _destSubTypes = [ .. info . PropertyType . GetTypeInfo ( ) . GenericTypeArguments , info . PropertyType ] ;
294296 else if ( propertyMap . SourceMember is FieldInfo fInfo )
295297 _destSubTypes = fInfo . FieldType . GetTypeInfo ( ) . GenericTypeArguments ;
296298 }
0 commit comments