コード例 #1
0
            private Expression VisitAllParametersExpression <T>(Expression <T> expression)
            {
                var visitors = (
                    from t in expression.Parameters
                    let sourceParamType = t.Type
                                          from destParamType in _destSubTypes.Where(dt => dt != sourceParamType)
                                          let a = destParamType.IsGenericType() ? destParamType.GetTypeInfo().GenericTypeArguments[0] : destParamType
                                                  let typeMap = _configurationProvider.FindTypeMapFor(a, sourceParamType)
                                                                where typeMap != null
                                                                let oldParam = t
                                                                               let newParam = Parameter(a, oldParam.Name)
                                                                                              select new MappingVisitor(_configurationProvider, typeMap, oldParam, newParam, this))
                               .Cast <ExpressionVisitor>()
                               .ToList();

                return(visitors.Aggregate(expression as Expression, (e, v) => v.Visit(e)));
            }
コード例 #2
0
        public static string GetMappedOriginField <TEntity, T>(IConfigurationProvider mapper, string fieldName)
        => mapper
        ?.FindTypeMapFor <TEntity, T>()
        ?.PropertyMaps
        ?.FirstOrDefault(x => fieldName.Equals(x.DestinationName, StringComparison.OrdinalIgnoreCase))
        ?.SourceMember
        ?.Name

        ?? fieldName;
コード例 #3
0
        public static TypeMap CheckIfMapExists(this IConfigurationProvider config, Type sourceType, Type destinationType)
        {
            var typeMap = config.FindTypeMapFor(sourceType, destinationType);

            if (typeMap == null)
            {
                throw MissingMapException(sourceType, destinationType);
            }
            return(typeMap);
        }
コード例 #4
0
            protected override Expression VisitUnary(UnaryExpression node)
            {
                if (node.NodeType == ExpressionType.Convert)
                {
                    var replacingExpression = Visit(node.Operand);
                    if (replacingExpression.Type != node.Operand.Type)
                    {
                        var typeMap = _configurationProvider.FindTypeMapFor(replacingExpression.Type, node.Type);
                        if (typeMap != null && typeMap.CustomProjection != null)
                        {
                            replacingExpression = typeMap.CustomProjection.ConvertReplaceParameters(replacingExpression);
                        }
                        else if (replacingExpression.Type.CanImplicitConvert(node.Type))
                        {
                            replacingExpression = Convert(replacingExpression, node.Type);
                        }

                        return(Convert(replacingExpression, node.Type));
                    }

                    //if (node.Type != node.Operand.Type)
                    //{
                    //    return Convert(node.Operand, node.Operand.Type);
                    //}

                    var call = node.Operand as MethodCallExpression;
                    if (call != null)
                    {
                        //var convertedMethod = GetConvertedMethodCall(call);

                        //if (convertedMethod.Type != node.Type)
                        //{
                        //    return Visit(Convert(convertedMethod, node.Type));
                        //}

                        //return Visit(convertedMethod);
                    }
                }

                return(base.VisitUnary(node));
            }
コード例 #5
0
            private Expression VisitAllParametersExpression<T>(Expression<T> expression)
            {
                var visitors = new List<ExpressionVisitor>();
                for (var i = 0; i < expression.Parameters.Count; i++)
                {
                    var sourceParamType = expression.Parameters[i].Type;
                    foreach (var destParamType in _destSubTypes.Where(dt => dt != sourceParamType))
                    {
                        var a = destParamType.IsGenericType() ? destParamType.GetTypeInfo().GenericTypeArguments[0]: destParamType;
                        var typeMap = _configurationProvider.FindTypeMapFor(a, sourceParamType);

                        if (typeMap == null)
                            continue;

                        var oldParam = expression.Parameters[i];
                        var newParam = Expression.Parameter(a, oldParam.Name);
                        visitors.Add(new MappingVisitor(_configurationProvider, typeMap, oldParam, newParam, this));
                    }
                }
                return visitors.Aggregate(expression as Expression, (e, v) => v.Visit(e));
            }
コード例 #6
0
    public static IMappingExpression <TSource, TDestination> InheritMappingFromBaseType <TSource, TDestination>(
        this IMappingExpression <TSource, TDestination> mappingExpression,
        WithBaseFor baseFor          = WithBaseFor.Both,
        IMappingEngine mappingEngine = null,
        IConfigurationProvider configurationProvider = null)
    {
        Type sourceType       = typeof(TSource);
        Type destinationType  = typeof(TDestination);
        Type sourceParentType = baseFor == WithBaseFor.Both || baseFor == WithBaseFor.Source
                ? sourceType.BaseType
                : sourceType;
        Type destinationParentType = baseFor == WithBaseFor.Both || baseFor == WithBaseFor.Destination
                ? destinationType.BaseType
                : destinationType;

        mappingExpression
        .BeforeMap((sourceObject, destObject) =>
        {
            if (mappingEngine != null)
            {
                mappingEngine.Map(sourceObject, destObject, sourceParentType, destinationParentType);
            }
            else
            {
                Mapper.Map(sourceObject, destObject, sourceParentType, destinationParentType);
            }
        });
        TypeMap baseTypeMap = configurationProvider != null
                ? configurationProvider.FindTypeMapFor(sourceParentType, destinationParentType)
                : Mapper.FindTypeMapFor(sourceParentType, destinationParentType);

        if (baseTypeMap == null)
        {
            throw new InvalidOperationException(
                      string.Format("Missing map from {0} to {1}.", new object[]
            {
                sourceParentType.Name,
                destinationParentType.Name
            }));
        }
        foreach (PropertyMap propertyMap in baseTypeMap.GetPropertyMaps())
        {
            mappingExpression.ForMember(propertyMap.DestinationProperty.Name, opt => opt.Ignore());
        }
        return(mappingExpression);
    }
        private bool canMap(Type srcType, Type destType)
        {
            if (destType.IsAssignableFrom(srcType))
            {
                return(true);
            }

            // This is based on code in MappingEngine's IMappingEngineRunner.Map method
            var context = new ResolutionContext(
                _mappingConfig.FindTypeMapFor(null, srcType, destType),
                null,
                srcType,
                destType
                );

            return(_mappingConfig.GetMappers().Any(mapper => mapper.IsMatch(context)));
        }
コード例 #8
0
 private static IEnumerable <TypeMap> GetIncludedTypeMaps(IEnumerable <TypePair> includedTypes, IConfigurationProvider configurationProvider)
 {
     foreach (var pair in includedTypes)
     {
         var typeMap = configurationProvider.FindTypeMapFor(pair);
         if (typeMap != null)
         {
             yield return(typeMap);
         }
         typeMap = configurationProvider.ResolveTypeMap(pair);
         // we want the exact map the user included, but we could instantiate an open generic
         if (typeMap == null || typeMap.Types != pair || typeMap.IsConventionMap)
         {
             throw QueryMapperHelper.MissingMapException(pair);
         }
         yield return(typeMap);
     }
 }
コード例 #9
0
        private void Configure(ITypeMapConfiguration typeMapConfiguration, IConfigurationProvider configurationProvider)
        {
            var typeMap = configurationProvider.FindTypeMapFor(typeMapConfiguration.Types);

            Configure(typeMap, configurationProvider);
        }
コード例 #10
0
 /// <summary>
 /// 是否已存在映射配置
 /// </summary>
 private static bool Exists(Type sourceType, Type destinationType)
 {
     return(_config?.FindTypeMapFor(sourceType, destinationType) != null);
 }
コード例 #11
0
 /// <summary>
 /// Determines whether [has map for] [the specified source].
 /// </summary>
 /// <param name="configuration">The configuration.</param>
 /// <param name="source">The source.</param>
 /// <param name="destination">The destination.</param>
 /// <param name="includeObjectMappers">if set to <c>true</c> [include object mappers].</param>
 /// <returns>
 ///   <c>true</c> if the ocnfiguration has map for the specified source and destination types; otherwise, <c>false</c>.
 /// </returns>
 public static bool HasMapFor(this IConfigurationProvider configuration, Type source, Type destination, bool includeObjectMappers = true)
 {
     return(source == destination ||
            (configuration.FindTypeMapFor(source, destination) != null) ||
            includeObjectMappers && configuration.CoveredByObjectMap(source, destination));
 }
コード例 #12
0
 /// <summary>
 /// 获取对应源对象和目标对象的映射TypeMap
 /// </summary>
 /// <param name="source"></param>
 /// <param name="destination"></param>
 /// <returns></returns>
 private static TypeMap GetTypeMap(Type source, Type destination)
 {
     return(_mapperConfig.FindTypeMapFor(source, destination));
 }
 private static void SetSealed(IConfigurationProvider cfg)
 {
     // If sealed _typeMapCache is filled and should be able to Resolve tye TypeMap
     // If not sealed _typeMapCache is empty and should return null
     _sealed = cfg.FindTypeMapFor(typeof(PlaceHolder), typeof(PlaceHolder)) != null;
 }