Example #1
0
 private static void CreateMap <TSource, TTarget>()
 {
     AutoMapper.TypeMap typeMap = AutoMapper.Mapper.FindTypeMapFor <TSource, TTarget>();
     if (typeMap == null)
     {
         AutoMapper.Mapper.CreateMap <TSource, TTarget>();
     }
 }
Example #2
0
        public string CreateMethodInnerCode(TypeMap map)
        {
            RememberTypeLocations(map);

            string srcFieldName  = "src";
            string destFieldName = "dest";

            var assignment = ProcessTypeMap(map, srcFieldName, destFieldName);

            return(assignment.Code);
        }
Example #3
0
        private Assignment ProcessTypeMap(TypeMap rootMap, string srcFieldName, string destFieldName)
        {
            var coder = new Coder();

            foreach (PropertyMap propertyMap in rootMap.PropertyMaps)
            {
                RememberTypeLocations(propertyMap);

                var context = new PropertyNameContext(propertyMap, srcFieldName, destFieldName);

                if (propertyMap.Ignored)
                {
                    continue;
                }

                //assign without explicit cast
                if (propertyMap.DestType.IsAssignableFrom(propertyMap.SrcType) ||
                    propertyMap.DestType.IsImplicitCastableFrom(propertyMap.SrcType))
                {
                    //TODO: need to determine explicit casts and produce cast operators
                    coder.SimpleAssign(context);
                    continue;
                }
                else
                {
                    bool referenceType = propertyMap.DestType.IsClass;
                    //TODO: perfomance degrades on each null check! Try to avoid it if possible!
                    if (referenceType)
                    {
                        coder.NullCheck(context);
                        coder.AttachRawCode(" else {{");

                        coder.AppendNoParameterlessCtorException(context, propertyMap.DestType);
                    }

                    ProcessPropertyTypePair(coder, context, propertyMap);

                    if (referenceType)
                    {
                        coder.AttachRawCode("}}");
                    }
                }
            }

            var assignment = coder.GetAssignment();

            TemplateCache.AddIfNotExist(rootMap.TypePair, assignment.RelativeTemplate);

            return(assignment);
        }
Example #4
0
 private void RememberTypeLocations(TypeMap typeMap)
 {
     DetectedLocations.Add(typeMap.SrcType.Assembly.Location);
     DetectedLocations.Add(typeMap.DestType.Assembly.Location);
 }