コード例 #1
0
 public static IMappingExpression <TSource, TDestination> IgnoreSource <TSource, TDestination>(
     this IMappingExpression <TSource, TDestination> map,
     Expression <Func <TSource, object> > selector)
 {
     map.ForSourceMember(selector, opt => opt.DoNotValidate());
     return(map);
 }
コード例 #2
0
    public static IMappingExpression <TSource, TDestination> IgnoreAllButMembersWithDataMemberAttribute <TSource, TDestination>(
        this IMappingExpression <TSource, TDestination> expression)
    {
        var sourceType      = typeof(TSource);
        var destinationType = typeof(TDestination);

        foreach (var property in sourceType.GetProperties())
        {
            var descriptor             = TypeDescriptor.GetProperties(sourceType)[property.Name];
            var hasDataMemberAttribute = descriptor.Attributes.OfType <DataMemberAttribute>().Any();
            if (!hasDataMemberAttribute)
            {
                expression.ForSourceMember(property.Name, opt => opt.Ignore());
            }
        }
        foreach (var property in destinationType.GetProperties())
        {
            var descriptor             = TypeDescriptor.GetProperties(destinationType)[property.Name];
            var hasDataMemberAttribute = descriptor.Attributes.OfType <DataMemberAttribute>().Any();
            if (!hasDataMemberAttribute)
            {
                expression.ForMember(property.Name, opt => opt.Ignore());
            }
        }
        return(expression);
    }
コード例 #3
0
        /// <summary>
        /// 如果type的属性成员定义了特性TAttribute,那么该属性映射被忽略
        /// </summary>
        /// <typeparam name="TAttribute"></typeparam>
        /// <param name="mappingExpression"></param>
        /// <param name="type"></param>
        protected void OnlyMap <TAttribute>(IMappingExpression mappingExpression, Type type, bool isSource = true)
            where TAttribute : Attribute
        {
            //AutoMap 的ForMember意思是,对于目标对象的XX成员,该如何映射
            //假如要让sourceType的XX属性不映射,那么应该是ForMember(XX,opt=>opt.Ignore)
            //然而这里的XX是目标对象的XX
            //那么如果目标对象没有XX,其实没必要调用ForMember

            //官方说道,ForSourceMember主要用于Validate https://github.com/AutoMapper/AutoMapper/issues/1556
            //真正的要ignore映射,还是应当用ForMember
            foreach (var item in type.GetProperties())
            {
                if (item.IsDefined(typeof(TAttribute)))
                {
                    if (isSource)
                    {
                        mappingExpression.ForSourceMember(item.Name, opt => opt.Ignore()).ReverseMap();
                    }
                    else
                    {
                        mappingExpression.ForMember(item.Name, opt => opt.Ignore());
                    }
                }
            }
        }
コード例 #4
0
 public static IMappingExpression IgnoreAllNonExistingSource(this IMappingExpression expression)
 {
     foreach (var property in expression.TypeMap.GetUnmappedPropertyNames())
     {
         expression.ForSourceMember(property, opt => opt.Ignore());
     }
     return(expression);
 }
コード例 #5
0
        public static IMappingExpression <TSource, TDest> IncludeAuthorName <TSource, TDest>(this IMappingExpression <TSource, TDest> map)
            where TSource : Revision
            where TDest : CommonResponse
        {
            map.ForMember(x => x.AuthorName, opt => opt.ResolveUsing <AuthorNameResolver>());
            map.ForSourceMember(x => x.Author, opt => opt.Ignore());

            return(map);
        }
コード例 #6
0
    public static IMappingExpression <TSource, TDestination> IgnoreAllSourceVirtual <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression)
    {
        var srcType = typeof(TSource);

        foreach (var property in srcType.GetProperties().Where(p => p.GetGetMethod().IsVirtual&& !p.GetGetMethod().IsFinal))
        {
            expression.ForSourceMember(property.Name, opt => opt.Ignore());
        }
        return(expression);
    }
コード例 #7
0
        public static IMappingExpression <TSource, TDest> IgnoreSource <TSource, TDest>(this IMappingExpression <TSource, TDest> expression, Expression <Func <TSource, object> > property)
        {
            var sourceName = PropertyUtil.GetName(property);

            if (typeof(TDest).GetProperty(sourceName) != null)
            {
                expression.ForMember(sourceName, opt => opt.Ignore());
            }
            expression.ForSourceMember(property, opt => opt.DoNotValidate());
            return(expression);
        }
コード例 #8
0
        /*
         * Source parametresi olarak geçilen objet  içersinde virtual property varsa ignore etmek için oluşturuldu
         */
        public static IMappingExpression <TSource, TDestination> IgnoreVirtualPropertySource <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression)
        {
            var desType = typeof(TDestination);

            foreach (var property in desType.GetProperties().Where(p =>
                                                                   p.GetGetMethod().IsVirtual))
            {
                expression.ForSourceMember(property.Name, options => options.Ignore());
            }
            return(expression);
        }
コード例 #9
0
        public static IMappingExpression <TSource, TDestination> IgnoreAllNonExistingSource <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType      = typeof(TSource);
            var destinationType = typeof(TDestination);
            var existingMaps    = AutoMapper.Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));

            foreach (var property in existingMaps.GetUnmappedPropertyNames())
            {
                expression.ForSourceMember(property, opt => opt.Ignore());
            }
            return(expression);
        }
コード例 #10
0
ファイル: MapperHelper.cs プロジェクト: senglory/Medcom
 private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
 {
     foreach (string propName in map.GetUnmappedPropertyNames())
     {
         if (map.SourceType.GetProperty(propName) != null)
         {
             expr.ForSourceMember(propName, opt => opt.Ignore());
         }
         if (map.DestinationType.GetProperty(propName) != null)
         {
             expr.ForMember(propName, opt => opt.Ignore());
         }
     }
 }
        public static IMappingExpression <TSource, TDestination> IgnoreMissingDestinationMembers <TSource, TDestination>
            (this IMappingExpression <TSource, TDestination> expression)
        {
            var destinationType  = typeof(TDestination);
            var sourceProperties = typeof(TSource).GetProperties();

            foreach (var property in sourceProperties)
            {
                if (destinationType.GetProperty(property.Name) == null)
                {
                    expression.ForSourceMember(property.Name, opt => opt.DoNotValidate());
                }
            }
            return(expression);
        }
コード例 #12
0
        public static void IgnoreAllPropertiesWithAnInaccessibleSetter(this IMappingExpression map, TypeMap type)
        {
            var properties = type.DestinationType.GetPropertiesWithInaccessibleSetter();

            foreach (var property in properties)
            {
                map.ForMember(property.Name, opt => opt.Ignore());
            }

            properties = type.SourceType.GetPropertiesWithInaccessibleSetter();
            foreach (var property in properties)
            {
                map.ForSourceMember(property.Name, opt => opt.Ignore());
            }
        }
コード例 #13
0
        private static void IgnoreUnmappedProperties(TypeMap map, IMappingExpression expr)
        {
            foreach (string propName in map.GetUnmappedPropertyNames())
            {
                var srcPropInfo = map.SourceType.GetProperty(propName);

                if (srcPropInfo != null)
                {
                    expr.ForSourceMember(propName, opt => opt.DoNotValidate());
                }

                var destPropInfo = map.DestinationType.GetProperty(propName);

                if (destPropInfo != null)
                {
                    expr.ForMember(propName, opt => opt.Ignore());
                }
            }
        }
コード例 #14
0
        public static IMappingExpression <TSource, TDestination> IgnoreAllNonExisting <TSource, TDestination>
            (this IMappingExpression <TSource, TDestination> expression)
        {
            var sourceType      = typeof(TSource);
            var destinationType = typeof(TDestination);
            var existingMaps    = expression.TypeMap;

            foreach (var property in existingMaps.GetUnmappedPropertyNames())
            {
                if (sourceType.GetProperty(property) != null)
                {
                    expression.ForSourceMember(property, o => o.Ignore());
                }
                if (destinationType.GetProperty(property) != null)
                {
                    expression.ForMember(property, opt => opt.Ignore());
                }
            }
            return(expression);
        }
コード例 #15
0
 public static IMappingExpression <TSource, TDestination> IgnoreSource <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression, Expression <Func <TSource, object> > sourceMember)
 {
     return(expression.ForSourceMember(sourceMember, opt => opt.Ignore()));
 }
コード例 #16
0
 public IObjectMapperBindingConfig <TFrom, TTo> Ignore(Expression <Func <TFrom, object> > expression)
 {
     mappingExpression.ForSourceMember(expression, opt => opt.Ignore());
     return(this);
 }
コード例 #17
0
 public static IMappingExpression <TSource, TDestination> IgnoreSourceMember <TSource, TDestination>(this IMappingExpression <TSource, TDestination> map, Expression <Func <TSource, object> > selector)
 {
     return(map.ForSourceMember(selector, config => config.DoNotValidate()));;
 }