Пример #1
0
        static MapperDescritor FindMapperDescritorForMapFromType(Type type)
        {
            TypeInfo typeInfo = type.GetTypeInfo();
            bool     hasMapFromTypeAttribute = typeInfo.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(MapFromMemberAttribute)) == false)
                {
                    continue;
                }

                MapFromMemberAttribute mapFromMemberAttribute = property.GetCustomAttribute <MapFromMemberAttribute>();

                PropertyInfo sourceMember = sourceType.GetProperty(mapFromMemberAttribute.MemberName);
                if (sourceMember == null)
                {
                    throw new Exception(string.Format("Source type '{0}' does not define property named '{1}'.", sourceType.FullName, mapFromMemberAttribute.MemberName));
                }

                MapperMemberRelationship memberRelationship = new MapperMemberRelationship(sourceMember, property);

                mapperDescritor.MemberRelationships.Add(memberRelationship);
            }

            return(mapperDescritor);
        }
Пример #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 = FindMapperDescritorForMapToType(exportedType);
                if (mapperDescritorOfMapToTypeAttr != null)
                {
                    ret.Add(mapperDescritorOfMapToTypeAttr);
                }

                MapperDescritor mapperDescritorOfMapFromTypeAttr = FindMapperDescritorForMapFromType(exportedType);
                if (mapperDescritorOfMapFromTypeAttr != null)
                {
                    ret.Add(mapperDescritorOfMapFromTypeAttr);
                }
            }

            return(ret);
        }