Example #1
0
        private Expression CreateNewDestinationFunc(out bool constructorMapping)
        {
            constructorMapping = false;
            if (_typeMap.DestinationCtor != null)
            {
                return(_typeMap.DestinationCtor.ReplaceParameters(Source, Context));
            }

            if (_typeMap.ConstructDestinationUsingServiceLocator)
            {
                return(CreateInstance(_typeMap.DestinationTypeToUse));
            }

            if (_typeMap.ConstructorMap?.CanResolve == true)
            {
                constructorMapping = true;
                return(CreateNewDestinationExpression(_typeMap.ConstructorMap));
            }
#if NET45 || NET40
            if (_typeMap.DestinationTypeToUse.IsInterface())
            {
                var ctor = Call(null,
                                typeof(DelegateFactory).GetDeclaredMethod(nameof(DelegateFactory.CreateCtor), new[] { typeof(Type) }),
                                Call(null,
                                     typeof(ProxyGenerator).GetDeclaredMethod(nameof(ProxyGenerator.GetProxyType)),
                                     Constant(_typeMap.DestinationTypeToUse)));
                // We're invoking a delegate here to make it have the right accessibility
                return(Invoke(ctor));
            }
#endif
            return(DelegateFactory.GenerateConstructorExpression(_typeMap.DestinationTypeToUse));
        }
Example #2
0
        private static Expression NullCheck(MemberExpression memberExpression)
        {
            var setter = GetSetter(memberExpression);
            var ifNull = setter == null ? (Expression)
                         Throw(Constant(new NullReferenceException($"{memberExpression} cannot be null because it's used by ForPath."))) :
                         Assign(setter, DelegateFactory.GenerateConstructorExpression(memberExpression.Type));

            return(memberExpression.IfNullElse(ifNull));
        }
Example #3
0
        private static Expression CreateNewDestinationFunc(
            TypeMap typeMap,
            TypeMapRegistry typeMapRegistry,
            ParameterExpression srcParam,
            ParameterExpression ctxtParam,
            ref ParameterExpression contextToReuse)
        {
            if (typeMap.DestinationCtor != null)
            {
                return(typeMap.DestinationCtor.ReplaceParameters(srcParam, ctxtParam));
            }

            if (typeMap.ConstructDestinationUsingServiceLocator)
            {
                return(Call(MakeMemberAccess(ctxtParam, typeof(ResolutionContext).GetProperty("Options")),
                            typeof(MappingOperationOptions).GetMethod("CreateInstance")
                            .MakeGenericMethod(typeMap.DestinationType)
                            ));
            }

            if (typeMap.ConstructorMap?.CanResolve == true)
            {
                return(typeMap.ConstructorMap.BuildExpression(typeMapRegistry, srcParam, ctxtParam, ref contextToReuse));
            }

            if (typeMap.DestinationType.IsInterface())
            {
#if PORTABLE
                Block(typeof(object),
                      Throw(
                          Constant(
                              new PlatformNotSupportedException("Mapping to interfaces through proxies not supported."))),
                      Constant(null));
#else
                var ctor = Call(Constant(ObjectCreator.DelegateFactory), typeof(DelegateFactory).GetMethod("CreateCtor", new[] { typeof(Type) }), Call(New(typeof(ProxyGenerator)), typeof(ProxyGenerator).GetMethod("GetProxyType"), Constant(typeMap.DestinationType)));
                return(Invoke(ctor));
#endif
            }

            if (typeMap.DestinationType.IsAbstract())
            {
                return(Constant(null));
            }

            if (typeMap.DestinationType.IsGenericTypeDefinition())
            {
                return(Constant(null));
            }

            return(DelegateFactory.GenerateConstructorExpression(typeMap.DestinationType));
        }
Example #4
0
        private static Expression CreateNewDestinationFunc(
            TypeMap typeMap,
            TypeMapRegistry typeMapRegistry,
            ParameterExpression srcParam,
            ParameterExpression ctxtParam)
        {
            if (typeMap.DestinationCtor != null)
            {
                return(typeMap.DestinationCtor.ReplaceParameters(srcParam, ctxtParam));
            }

            if (typeMap.ConstructDestinationUsingServiceLocator)
            {
                return(Call(MakeMemberAccess(ctxtParam, typeof(ResolutionContext).GetProperty("Options")),
                            typeof(MappingOperationOptions).GetMethod("CreateInstance")
                            .MakeGenericMethod(typeMap.DestinationTypeToUse)
                            ));
            }

            if (typeMap.ConstructorMap?.CanResolve == true)
            {
                return(typeMap.ConstructorMap.BuildExpression(typeMapRegistry, srcParam, ctxtParam));
            }

#if NET45
            if (typeMap.DestinationTypeToUse.IsInterface())
            {
                var ctor = Call(Constant(ObjectCreator.DelegateFactory), typeof(DelegateFactory).GetMethod("CreateCtor", new[] { typeof(Type) }), Call(New(typeof(ProxyGenerator)), typeof(ProxyGenerator).GetMethod("GetProxyType"), Constant(typeMap.DestinationTypeToUse)));
                // We're invoking a delegate here
                return(Invoke(ctor));
            }
#endif

            if (typeMap.DestinationTypeToUse.IsAbstract())
            {
                return(Constant(null));
            }

            if (typeMap.DestinationTypeToUse.IsGenericTypeDefinition())
            {
                return(Constant(null));
            }

            return(DelegateFactory.GenerateConstructorExpression(typeMap.DestinationTypeToUse));
        }
        private Expression CreateInnerObjects(PathMap pathMap, Expression destination)
        {
            var memberExpression = (MemberExpression)destination;
            var nullChecks       = new List <Expression>();

            while (memberExpression != null)
            {
                var setter = GetSetter(memberExpression);
                var ifNull = setter == null ? (Expression)
                             Throw(Constant(new NullReferenceException($"{memberExpression} cannot be null because it's used by ForPath."))) :
                             Assign(setter, DelegateFactory.GenerateConstructorExpression(memberExpression.Type));
                var nullCeck = memberExpression.IfNullElse(ifNull);
                nullChecks.Add(nullCeck);
                memberExpression = memberExpression.Expression as MemberExpression;
            }
            nullChecks.Reverse();
            return(Block(nullChecks));
        }
        private Expression CreateNewDestinationFunc(out bool constructorMapping)
        {
            constructorMapping = false;
            if (_typeMap.DestinationCtor != null)
            {
                return(_typeMap.DestinationCtor.ReplaceParameters(_source, _context));
            }

            if (_typeMap.ConstructDestinationUsingServiceLocator)
            {
                return(Call(MakeMemberAccess(_context, typeof(ResolutionContext).GetDeclaredProperty("Options")),
                            typeof(IMappingOperationOptions).GetDeclaredMethod("CreateInstance")
                            .MakeGenericMethod(_typeMap.DestinationTypeToUse)
                            ));
            }

            if (_typeMap.ConstructorMap?.CanResolve == true)
            {
                constructorMapping = true;
                return(_typeMap.ConstructorMap.BuildExpression(this));
            }
#if NET45
            if (_typeMap.DestinationTypeToUse.IsInterface())
            {
                var ctor = Call(Constant(ObjectCreator.DelegateFactory), typeof(DelegateFactory).GetDeclaredMethod("CreateCtor", new[] { typeof(Type) }), Call(New(typeof(ProxyGenerator)), typeof(ProxyGenerator).GetDeclaredMethod("GetProxyType"), Constant(_typeMap.DestinationTypeToUse)));
                // We're invoking a delegate here
                return(Invoke(ctor));
            }
#endif

            if (_typeMap.DestinationTypeToUse.IsAbstract())
            {
                return(Constant(null));
            }

            if (_typeMap.DestinationTypeToUse.IsGenericTypeDefinition())
            {
                return(Constant(null));
            }

            return(DelegateFactory.GenerateConstructorExpression(_typeMap.DestinationTypeToUse));
        }