Exemple #1
0
        static MapperDescritor findMapperDescritorForMapFromType(Type type)
        {
            TypeInfo typeInfo = type.GetTypeInfo();
            bool     hasMapFromTypeAttribute = type.IsDefined(typeof(MapFromTypeAttribute));

            if (!hasMapFromTypeAttribute)
            {
                return(null);
            }
            MapFromTypeAttribute mapFromTypeAttribute = typeInfo.GetCustomAttribute <MapFromTypeAttribute>();
            Type            sourceType      = mapFromTypeAttribute.type;
            Type            targetType      = type;
            bool            reverseMap      = mapFromTypeAttribute.ReverseMap;
            MapperDescritor mapperDescritor = new MapperDescritor(sourceType, targetType, reverseMap);
            var             properties      = typeInfo.GetProperties();

            foreach (var property in properties)
            {
                if (property.IsDefined(typeof(MapFromTypeAttribute)) == false)
                {
                    continue;
                }

                MapFromMemberAttribute mapFromMemberAttribute = property.GetCustomAttribute <MapFromMemberAttribute>();
                PropertyInfo           sourceMember           = sourceType.GetProperty(mapFromMemberAttribute.MemberName);
                if (sourceMember == null)
                {
                    throw new Exception($"Source type '{sourceType.FullName}' does define property named '{mapFromMemberAttribute.MemberName}',");
                }
                MapperMemberRelationship memberRelationship = new MapperMemberRelationship(sourceType, property);
                mapperDescritor.MemberRelationships.Add(memberRelationship);
            }
            return(mapperDescritor);
        }
Exemple #2
0
        static List <MapperDescritor> FindMapperDescritors(Assembly assembly)
        {
            IEnumerable <Type>     exportedTypes = assembly.ExportedTypes;
            List <MapperDescritor> ret           = new List <MapperDescritor>();

            foreach (Type exportedType in exportedTypes)
            {
                MapperDescritor mapperDescritorOfMapToTypeAttr = FindMapperdDescritorForMapToType(exportedType);
                if (mapperDescritorOfMapToTypeAttr != null)
                {
                    ret.Add(mapperDescritorOfMapToTypeAttr);
                }
                MapperDescritor mapperDescritorOfMapFromTypeAttr = findMapperDescritorForMapFromType(exportedType);
                if (mapperDescritorOfMapFromTypeAttr != null)
                {
                    ret.Add(mapperDescritorOfMapFromTypeAttr);
                }
            }
            return(ret);
        }
Exemple #3
0
        static MapperDescritor FindMapperdDescritorForMapToType(Type type)
        {
            TypeInfo typeInfo = type.GetTypeInfo();
            bool     hasMapToTypeAttribute = typeInfo.IsDefined(typeof(MapToTypeAttribute));

            if (!hasMapToTypeAttribute)
            {
                return(null);
            }
            MapToTypeAttribute mapToTypeAttribute = typeInfo.GetCustomAttribute <MapToTypeAttribute>();
            Type            sourceType            = type;
            Type            targetType            = mapToTypeAttribute.Type;
            bool            reverseMap            = mapToTypeAttribute.ReverseMap;
            MapperDescritor mapperDescritor       = new MapperDescritor(sourceType, targetType, reverseMap);
            var             properties            = typeInfo.GetProperties();

            foreach (var property in properties)
            {
                if (property.IsDefined(typeof(MapToMemberAttribute)) == false)
                {
                    continue;
                }
                MapToMemberAttribute mapToMemberAttribute = property.GetCustomAttribute <MapToMemberAttribute>();
                PropertyInfo         targetMember         = targetType.GetProperty(mapToMemberAttribute.MemberName);
                if (targetMember == null)
                {
                    throw new Exception(string.Format("Target type '{0}' does not define property named '{1}'.", targetType.FullName, mapToMemberAttribute.MemberName));
                }

                MapperMemberRelationship memberRelationship = new MapperMemberRelationship(property, targetMember);

                mapperDescritor.MemberRelationships.Add(memberRelationship);
            }

            return(mapperDescritor);
        }