当前位置: 首页>>代码示例>>C#>>正文


C# IConfigurationProvider.ResolveTypeMap方法代码示例

本文整理汇总了C#中IConfigurationProvider.ResolveTypeMap方法的典型用法代码示例。如果您正苦于以下问题:C# IConfigurationProvider.ResolveTypeMap方法的具体用法?C# IConfigurationProvider.ResolveTypeMap怎么用?C# IConfigurationProvider.ResolveTypeMap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IConfigurationProvider的用法示例。


在下文中一共展示了IConfigurationProvider.ResolveTypeMap方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateExpression

        public Expression CreateExpression(IConfigurationProvider configuration, ParameterExpression srcParam, ParameterExpression ctxtParam)
        {
            if (CustomExpression != null)
                return CustomExpression.ConvertReplaceParameters(srcParam).IfNotNull(DestinationType);

            if (CustomValueResolver != null)
            {
                // Invoking a delegate
                return Invoke(Constant(CustomValueResolver), srcParam, ctxtParam);
            }

            if (Parameter.IsOptional && (!SourceMembers.Any() || configuration.ResolveTypeMap(SourceType, DestinationType) == null))
            {
                DefaultValue = true;
                return Constant(Parameter.GetDefaultValue());
            }

            var valueResolverExpr = SourceMembers.Aggregate(
                (Expression) srcParam,
                (inner, getter) => getter is MethodInfo
                    ? getter.IsStatic()
                        ? Call(null, (MethodInfo) getter, inner)
                        : (Expression) Call(inner, (MethodInfo) getter)
                    : MakeMemberAccess(getter.IsStatic() ? null : inner, getter)
                );
            valueResolverExpr = valueResolverExpr.IfNotNull(DestinationType);

            if ((SourceType.IsEnumerableType() && SourceType != typeof (string))
                || configuration.ResolveTypeMap(SourceType, DestinationType) != null
                || !DestinationType.IsAssignableFrom(SourceType))
            {
                /*
                var value = context.Mapper.Map(result, null, sourceType, destinationType, context);
                 */
                return TypeMapPlanBuilder.ContextMap(valueResolverExpr, Default(DestinationType), ctxtParam, DestinationType);
            }
            return valueResolverExpr;
        }
开发者ID:tamirdresher,项目名称:AutoMapper,代码行数:38,代码来源:ConstructorParameterMap.cs

示例2: MapCollectionExpression

        internal static Expression MapCollectionExpression(this TypeMapRegistry typeMapRegistry,
           IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression,
           Expression destExpression, Expression contextExpression, Func<Expression, Expression> conditionalExpression, Type ifInterfaceType, MapItem mapItem)
        {
            var passedDestination = Variable(destExpression.Type, "passedDestination");
            var condition = conditionalExpression(passedDestination);
            var newExpression = Variable(passedDestination.Type, "collectionDestination");
            var sourceElementType = TypeHelper.GetElementType(sourceExpression.Type);
            var itemParam = Parameter(sourceElementType, "item");

            var itemExpr = mapItem(typeMapRegistry, configurationProvider, propertyMap, sourceExpression.Type, passedDestination.Type, itemParam, contextExpression);

            var destinationElementType = TypeHelper.GetElementType(destExpression.Type);
            var destinationCollectionType = typeof(ICollection<>).MakeGenericType(destinationElementType);
            var clearMethod = destinationCollectionType.GetDeclaredMethod("Clear");
            var cast = typeof(Enumerable).GetDeclaredMethod("Cast").MakeGenericMethod(itemParam.Type);
            var addMethod = destinationCollectionType.GetDeclaredMethod("Add");
            var genericSource = sourceExpression.Type.IsGenericType() ? sourceExpression : Call(null, cast, sourceExpression);
            var destination = propertyMap?.UseDestinationValue == true ? passedDestination : newExpression;
            var addItems = ForEach(genericSource, itemParam, Call(destination, addMethod, itemExpr));

            var mapExpr = Block(addItems, destination);

            var ifNullExpr = configurationProvider.Configuration.AllowNullCollections ? Constant(null, passedDestination.Type) : (Expression) newExpression;
            var checkNull =  
                Block(new[] { newExpression, passedDestination },
                    Assign(passedDestination, destExpression),
                    IfThenElse(condition ?? Constant(false),
                                    Block(Assign(newExpression, passedDestination), Call(newExpression, clearMethod)),
                                    Assign(newExpression, passedDestination.Type.NewExpr(ifInterfaceType))),
                    Condition(Equal(sourceExpression, Constant(null)), ToType(ifNullExpr, passedDestination.Type), ToType(mapExpr, passedDestination.Type))
                );
            if(propertyMap != null)
            {
                return checkNull;
            }
            var elementTypeMap = configurationProvider.ResolveTypeMap(sourceElementType, destinationElementType);
            if(elementTypeMap == null)
            {
                return checkNull;
            }
            var checkContext = TypeMapPlanBuilder.CheckContext(elementTypeMap, contextExpression);
            if(checkContext == null)
            {
                return checkNull;
            }
            return Block(checkContext, checkNull);
        }
开发者ID:gentledepp,项目名称:AutoMapper,代码行数:48,代码来源:CollectionMapper.cs

示例3: MapExpression

 public static Expression MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider,
     TypePair typePair, Expression sourceParameter, Expression contextParameter, PropertyMap propertyMap = null, Expression destinationParameter = null)
 {
     if(destinationParameter == null)
     {
         destinationParameter = Default(typePair.DestinationType);
     }
     var typeMap = configurationProvider.ResolveTypeMap(typePair);
     if(typeMap != null)
     {
         if(!typeMap.HasDerivedTypesToInclude())
         {
             typeMap.Seal(typeMapRegistry, configurationProvider);
             if(typeMap.MapExpression != null)
             {
                 return typeMap.MapExpression.ConvertReplaceParameters(sourceParameter, destinationParameter, contextParameter);
             }
             else
             {
                 return ContextMap(typePair, sourceParameter, contextParameter, destinationParameter);
             }
         }
         else
         {
             return ContextMap(typePair, sourceParameter, contextParameter, destinationParameter);
         }
     }
     var match = configurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(typePair));
     if(match != null)
     {
         var mapperExpression = match.MapExpression(typeMapRegistry, configurationProvider, propertyMap, sourceParameter, destinationParameter, contextParameter);
         return ToType(mapperExpression, typePair.DestinationType);
     }
     return ContextMap(typePair, sourceParameter, contextParameter, destinationParameter);
 }
开发者ID:tlycken,项目名称:AutoMapper,代码行数:35,代码来源:TypeMapPlanBuilder.cs

示例4: MapExpression

 private static Expression MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider,
     PropertyMap propertyMap, Expression itemParam, Expression contextParam, TypePair typePair)
 {
     Expression itemExpr;
     var typeMap = configurationProvider.ResolveTypeMap(typePair);
     if (typeMap != null && (typeMap.TypeConverterType != null || typeMap.CustomMapper != null))
     {
         if (!typeMap.Sealed)
             typeMap.Seal(typeMapRegistry, configurationProvider);
         return typeMap.MapExpression.ReplaceParameters(itemParam, Default(typePair.DestinationType), contextParam);
     }
     var match = configurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(typePair));
     if (match != null && typeMap == null)
     {
         itemExpr =
             ToType(
                 match.MapExpression(typeMapRegistry, configurationProvider, propertyMap, itemParam,
                     Default(typePair.DestinationType), contextParam), typePair.DestinationType);
     }
     else
     {
         var mapMethod =
             typeof (ResolutionContext).GetDeclaredMethods()
                 .First(m => m.Name == "Map")
                 .MakeGenericMethod(typePair.SourceType, typePair.DestinationType);
         itemExpr = Call(contextParam, mapMethod, itemParam, Default(typePair.DestinationType));
     }
     return itemExpr;
 }
开发者ID:284247028,项目名称:AutoMapper,代码行数:29,代码来源:CollectionMapper.cs

示例5: MapCollectionExpression

        internal static Expression MapCollectionExpression(this TypeMapRegistry typeMapRegistry,
           IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression,
           Expression destExpression, Expression contextExpression, Func<Expression, Expression> conditionalExpression, Type ifInterfaceType, MapItem mapItem)
        {
            var passedDestination = Variable(destExpression.Type, "passedDestination");
            var newExpressionValue = passedDestination.NewIfConditionFails(conditionalExpression, ifInterfaceType);
            var newExpression = Variable(newExpressionValue.Type, "collectionDestination");
            var sourceElementType = TypeHelper.GetElementType(sourceExpression.Type);
            var itemParam = Parameter(sourceElementType, "item");
            var itemExpr = mapItem(typeMapRegistry, configurationProvider, propertyMap, sourceExpression.Type, passedDestination.Type, itemParam, contextExpression);

            var blockExpressions = new List<Expression>();
            var blockVariables = new List<ParameterExpression> { newExpression, passedDestination };
            Expression destination;
            var destinationElementType = itemExpr.Type;
            var destinationCollectionType = typeof(ICollection<>).MakeGenericType(destinationElementType);
            var clearMethod = destinationCollectionType.GetDeclaredMethod("Clear");
            if(passedDestination.Type.IsCollectionType())
            {
                if(propertyMap == null)
                {
                    destination = newExpression;
                    blockExpressions.Add(IfThenElse(NotEqual(passedDestination, Constant(null)),
                        Call(passedDestination, clearMethod),
                        Assign(destination, newExpression)
                        ));
                }
                else if(propertyMap.UseDestinationValue)
                {
                    destination = passedDestination;
                    blockExpressions.Add(Call(passedDestination, clearMethod));
                }
                else
                {
                    destination = newExpression;
                }
            }
            else
            {
                destination = newExpression;
            }

            var cast = typeof(Enumerable).GetTypeInfo().DeclaredMethods.First(_ => _.Name == "Cast").MakeGenericMethod(itemParam.Type);

            var addMethod = destinationCollectionType.GetDeclaredMethod("Add");
            var genericSource = sourceExpression.Type.GetTypeInfo().IsGenericType ? sourceExpression : Call(null, cast, sourceExpression);
            blockExpressions.Add(ForEach(genericSource, itemParam, Call(
                destination,
                addMethod,
                itemExpr)));

            blockExpressions.Add(destination);

            var mapExpr = Block(blockExpressions);

            var ifNullExpr = configurationProvider.Configuration.AllowNullCollections ? Constant(null, passedDestination.Type) : (Expression) newExpression;
            var checkNull = Block(blockVariables,
                Assign(passedDestination, destExpression),
                Assign(newExpression, newExpressionValue),
                Condition(Equal(sourceExpression, Constant(null)), ToType(ifNullExpr, passedDestination.Type), ToType(mapExpr, passedDestination.Type))
                );
            if(propertyMap != null)
            {
                return checkNull;
            }
            var elementTypeMap = configurationProvider.ResolveTypeMap(sourceElementType, destinationElementType);
            if(elementTypeMap == null)
            {
                return checkNull;
            }
            var checkContext = TypeMapPlanBuilder.CheckContext(elementTypeMap, contextExpression);
            if(checkContext == null)
            {
                return checkNull;
            }
            return Block(checkContext, checkNull);
        }
开发者ID:tlycken,项目名称:AutoMapper,代码行数:77,代码来源:CollectionMapper.cs

示例6: CreatePropertyMapFunc

        private static Expression CreatePropertyMapFunc(
            PropertyMap propertyMap,
            IConfigurationProvider configurationProvider,
            TypeMapRegistry typeMapRegistry,
            ParameterExpression srcParam,
            ParameterExpression destParam,
            ParameterExpression ctxtParam)
        {
            var destMember = MakeMemberAccess(destParam, propertyMap.DestinationProperty.MemberInfo);

            Expression getter;

            var pi = propertyMap.DestinationProperty.MemberInfo as PropertyInfo;
            if (pi != null && pi.GetGetMethod(true) == null)
            {
                getter = Default(propertyMap.DestinationPropertyType);
            }
            else
            {
                getter = destMember;
            }

            var destValueExpr = propertyMap.UseDestinationValue
                ? getter
                : Default(propertyMap.DestinationPropertyType);

            var valueResolverExpr = BuildValueResolverFunc(propertyMap, typeMapRegistry, srcParam, getter, ctxtParam);

            if (propertyMap.SourceType != null && propertyMap.DestinationPropertyType != null)
            {
                var typePair = new TypePair(propertyMap.SourceType, propertyMap.DestinationPropertyType);
                var typeMap = configurationProvider.ResolveTypeMap(typePair);
                var match = configurationProvider.GetMappers().FirstOrDefault(m => m.IsMatch(typePair));
                if (typeMap != null && (typeMap.TypeConverterType != null || typeMap.CustomMapper != null))
                {
                    if(!typeMap.Sealed)
                        typeMap.Seal(typeMapRegistry, configurationProvider);
                    valueResolverExpr = typeMap.MapExpression.ConvertReplaceParameters(valueResolverExpr, destValueExpr, ctxtParam);
                }
                else if (match != null && typeMap == null)
                {
                    valueResolverExpr = match.MapExpression(typeMapRegistry, configurationProvider,
                        propertyMap, valueResolverExpr, destValueExpr,
                        ctxtParam);
                }
                else
                {
                    valueResolverExpr = SetMap(propertyMap, valueResolverExpr, destValueExpr, ctxtParam);
                }
            }
            else
            {
                valueResolverExpr = SetMap(propertyMap, valueResolverExpr, destValueExpr, ctxtParam);
            }

            if (propertyMap.Condition != null)
            {
                valueResolverExpr =
                    Condition(
                        propertyMap.Condition.ConvertReplaceParameters(
                            srcParam,
                            destParam,
                            ToType(valueResolverExpr, propertyMap.Condition.Parameters[2].Type),
                            ToType(getter, propertyMap.Condition.Parameters[2].Type),
                            ctxtParam
                        ),
                        ToType(valueResolverExpr, propertyMap.DestinationPropertyType),
                        getter
                    );
            }

            Expression mapperExpr;
            if (propertyMap.DestinationProperty.MemberInfo is FieldInfo)
            {
                mapperExpr = propertyMap.SourceType != propertyMap.DestinationPropertyType
                    ? Assign(destMember, ToType(valueResolverExpr, propertyMap.DestinationPropertyType))
                    : Assign(getter, valueResolverExpr);
            }
            else
            {
                var setter = ((PropertyInfo) propertyMap.DestinationProperty.MemberInfo).GetSetMethod(true);
                if (setter == null)
                {
                    mapperExpr = valueResolverExpr;
                }
                else
                {
                    mapperExpr = Assign(destMember, propertyMap.SourceType != propertyMap.DestinationPropertyType
                        ? ToType(valueResolverExpr, propertyMap.DestinationPropertyType)
                        : valueResolverExpr);
                }
            }

            if (propertyMap.PreCondition != null)
            {
                mapperExpr = IfThen(
                    propertyMap.PreCondition.ConvertReplaceParameters(srcParam, ctxtParam),
                    mapperExpr
                    );
            }
//.........这里部分代码省略.........
开发者ID:284247028,项目名称:AutoMapper,代码行数:101,代码来源:TypeMapPlanBuilder.cs


注:本文中的IConfigurationProvider.ResolveTypeMap方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。