11using System ;
22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
4- using System . Linq ;
54using System . Linq . Expressions ;
65using System . Reflection ;
76using AutoMapper . Collection ;
@@ -16,30 +15,30 @@ public static Type GetSinglePredicateExpressionArgumentType(this Type type)
1615 {
1716 return _singleParameterTypeDictionary . GetOrAdd ( type , t =>
1817 {
19- var isExpression = typeof ( Expression ) . GetTypeInfo ( ) . IsAssignableFrom ( t . GetTypeInfo ( ) ) ;
18+ var isExpression = typeof ( Expression ) . GetTypeInfo ( ) . IsAssignableFrom ( t . GetTypeInfo ( ) ) ;
2019 if ( ! isExpression )
2120 return null ;
2221
23- var expressionOf = t . GetTypeInfo ( ) . GenericTypeArguments . First ( ) ;
24- var isFunction = expressionOf . GetGenericTypeDefinition ( ) == typeof ( Func < , > ) ;
22+ var expressionOf = t . GetTypeInfo ( ) . GenericTypeArguments [ 0 ] ;
23+ var isFunction = expressionOf . GetGenericTypeDefinition ( ) == typeof ( Func < , > ) ;
2524 if ( ! isFunction )
2625 return null ;
2726
28- var isPredicate = expressionOf . GetTypeInfo ( ) . GenericTypeArguments [ 1 ] == typeof ( bool ) ;
27+ var isPredicate = expressionOf . GetTypeInfo ( ) . GenericTypeArguments [ 1 ] == typeof ( bool ) ;
2928 if ( ! isPredicate )
3029 return null ;
3130
32- var objType = expressionOf . GetTypeInfo ( ) . GenericTypeArguments . First ( ) ;
31+ var objType = expressionOf . GetTypeInfo ( ) . GenericTypeArguments [ 0 ] ;
3332 return CacheAndReturnType ( type , objType ) ;
3433 } ) ;
3534 }
3635
3736 private static Type CacheAndReturnType ( Type type , Type objType )
3837 {
39- _singleParameterTypeDictionary . AddOrUpdate ( type , objType , ( t , t2 ) => objType ) ;
38+ _singleParameterTypeDictionary . AddOrUpdate ( type , objType , ( _ , __ ) => objType ) ;
4039 return objType ;
4140 }
42-
41+
4342 public static Expression < Func < T , int > > GetHashCodeExpression < T > ( this List < Expression > members , ParameterExpression sourceParam )
4443 {
4544 var hashMultiply = Expression . Constant ( 397L ) ;
@@ -48,20 +47,30 @@ public static Expression<Func<T, int>> GetHashCodeExpression<T>(this List<Expres
4847 var returnTarget = Expression . Label ( typeof ( int ) ) ;
4948 var returnExpression = Expression . Return ( returnTarget , Expression . Convert ( hashVariable , typeof ( int ) ) , typeof ( int ) ) ;
5049 var returnLabel = Expression . Label ( returnTarget , Expression . Constant ( - 1 ) ) ;
51-
50+
5251 var expressions = new List < Expression > ( ) ;
5352 foreach ( var member in members )
5453 {
55- var callGetHashCode = Expression . Call ( member , member . Type . GetDeclaredMethod ( nameof ( GetHashCode ) ) ) ;
56- var convertHashCodeToInt64 = Expression . Convert ( callGetHashCode , typeof ( long ) ) ;
54+ // Call the GetHashCode method
55+ var hasCodeExpression = Expression . Convert ( Expression . Call ( member , member . Type . GetDeclaredMethod ( nameof ( GetHashCode ) ) ) , typeof ( long ) ) ;
56+
57+ // return (((object)x) == null ? 0 : x.GetHashCode())
58+ var hashCodeReturnTarget = Expression . Label ( typeof ( long ) ) ;
59+ var hashCode = Expression . Block (
60+ Expression . IfThenElse (
61+ Expression . ReferenceEqual ( Expression . Convert ( member , typeof ( object ) ) , Expression . Constant ( null ) ) ,
62+ Expression . Return ( hashCodeReturnTarget , Expression . Constant ( 0L , typeof ( long ) ) ) ,
63+ Expression . Return ( hashCodeReturnTarget , hasCodeExpression ) ) ,
64+ Expression . Label ( hashCodeReturnTarget , Expression . Constant ( 0L , typeof ( long ) ) ) ) ;
65+
5766 if ( expressions . Count == 0 )
5867 {
59- expressions . Add ( Expression . Assign ( hashVariable , convertHashCodeToInt64 ) ) ;
68+ expressions . Add ( Expression . Assign ( hashVariable , hashCode ) ) ;
6069 }
6170 else
6271 {
6372 var oldHashMultiplied = Expression . Multiply ( hashVariable , hashMultiply ) ;
64- var xOrHash = Expression . ExclusiveOr ( oldHashMultiplied , convertHashCodeToInt64 ) ;
73+ var xOrHash = Expression . ExclusiveOr ( oldHashMultiplied , hashCode ) ;
6574 expressions . Add ( Expression . Assign ( hashVariable , xOrHash ) ) ;
6675 }
6776 }
0 commit comments