11using System ;
22using System . Linq . Expressions ;
33using System . Reflection ;
4+ using AutoMapper . Execution ;
45
56namespace AutoMapper . EquivilencyExpression
67{
@@ -12,61 +13,33 @@ static EquivilentExpression()
1213 {
1314 BadValue = new EquivilentExpression ( ) ;
1415 }
15-
16- public bool IsEquivlent ( object source , object destination )
17- {
18- throw new NotImplementedException ( ) ;
19- }
2016 }
2117
22- internal class EquivilentExpression < TSource , TDestination > : IEquivilentExpression , IToSingleSourceEquivalentExpression
18+ internal class EquivilentExpression < TSource , TDestination > : IEquivilentExpression < TSource , TDestination >
2319 where TSource : class
2420 where TDestination : class
2521 {
2622 private readonly Expression < Func < TSource , TDestination , bool > > _equivilentExpression ;
23+ private readonly Func < TSource , TDestination , bool > _equivilentFunc ;
2724
2825 public EquivilentExpression ( Expression < Func < TSource , TDestination , bool > > equivilentExpression )
2926 {
3027 _equivilentExpression = equivilentExpression ;
28+ _equivilentFunc = _equivilentExpression . Compile ( ) ;
3129 }
3230
33- public bool IsEquivlent ( object source , object destination )
31+ public bool IsEquivlent ( TSource source , TDestination destination )
3432 {
35- if ( ! ( source is TSource ) )
36- throw new EquivilentExpressionNotOfTypeException ( source . GetType ( ) , typeof ( TSource ) ) ;
37- if ( ! ( destination is TDestination ) )
38- throw new EquivilentExpressionNotOfTypeException ( destination . GetType ( ) , typeof ( TDestination ) ) ;
39- return _equivilentExpression . Compile ( ) ( source as TSource , destination as TDestination ) ;
33+ return _equivilentFunc ( source , destination ) ;
4034 }
4135
42- public Expression ToSingleSourceExpression ( object source )
36+ public Expression < Func < TDestination , bool > > ToSingleSourceExpression ( TSource source )
4337 {
4438 if ( source == null )
4539 throw new Exception ( "Invalid somehow" ) ;
46- return GetSourceOnlyExpresison ( source as TSource ) ;
47- }
4840
49- internal Expression GetSourceOnlyExpresison ( TSource source )
50- {
5141 var expression = new ParametersToConstantVisitor < TSource > ( source ) . Visit ( _equivilentExpression ) as LambdaExpression ;
52- return Expression . Lambda ( expression . Body , _equivilentExpression . Parameters [ 1 ] ) ;
53- }
54-
55- private class EquivilentExpressionNotOfTypeException : Exception
56- {
57- private readonly Type _objectType ;
58- private readonly Type _expectedType ;
59-
60- public EquivilentExpressionNotOfTypeException ( Type objectType , Type expectedType )
61- {
62- _objectType = objectType ;
63- _expectedType = expectedType ;
64- }
65-
66- public override string Message
67- {
68- get { return string . Format ( "{0} does not equal or inherit from {1}" , _objectType . Name , _expectedType . Name ) ; }
69- }
42+ return Expression . Lambda < Func < TDestination , bool > > ( expression . Body , _equivilentExpression . Parameters [ 1 ] ) ;
7043 }
7144 }
7245
0 commit comments