private string AssignReferenceTypes(PropertyNameContext ctx) { Recorder recorder = new Recorder(); //has parameterless ctor if (ctx.DestType.HasParameterlessCtor()) { recorder.AppendLine($"{{1}} = {StatementTemplates.New(ctx.DestTypeFullName)};"); } else { throw new HappyMapperException(ErrorMessages.NoParameterlessCtor(ctx.DestType)); } string template; //typepair isn't in template cache if (!TemplateCache.TryGetValue(ctx.TypePair, out template)) { var nodeMap = GetTypeMap(ctx.TypePair); var assignment = ProcessTypeMap(nodeMap); template = assignment.RelativeTemplate; } recorder.AppendLine(template); return(recorder.ToAssignment().RelativeTemplate); }
public static IPropertyNameContext CreateWithoutPropertyMap(Type srcType, Type destType, string srcPropertyName, string destPropertyName) { var ctx = new PropertyNameContext(); ctx.SrcType = srcType; ctx.DestType = destType; ctx.SrcMemberName = srcPropertyName; ctx.DestMemberName = destPropertyName; ctx.SrcTypeFullName = srcType.FullName.NormalizeTypeName(); ctx.DestTypeFullName = destType.FullName.NormalizeTypeName(); return(ctx); }
public ConditionPrinter(PropertyNameContext context, Recorder recorder) { Context = context; Recorder = recorder; var condition = Context.PropertyMap?.OriginalCondition?.Expression; IsExist = condition != null; if (IsExist) { string template = ToTemplate(condition); Recorder.AppendLine($"if ({template})"); Recorder.AppendLine("{{"); } }
public ConditionPrinterV2(PropertyNameContext context, Recorder recorder) { NameConvention = NameConventionsStorage.Condition; Context = context; Recorder = recorder; var condition = Context.PropertyMap.OriginalCondition; IsExist = condition != null; if (IsExist) { string template = ToTemplate(condition); Recorder.AppendLine($"if ({template})"); Recorder.AppendLine("{{"); } }
private Assignment ProcessTypeMap(TypeMap rootMap) { var recorder = new Recorder(); using (var bfm = new BeforeMapPrinter(new TypeNameContext(rootMap), recorder)) { } foreach (PropertyMap propertyMap in rootMap.PropertyMaps) { if (propertyMap.Ignored) { continue; } RememberTypeLocations(propertyMap); var ctx = new PropertyNameContext(propertyMap); //using (var condition = new ConditionPrinter(context, Recorder)) using (var condition = new ConditionPrinterV2(ctx, recorder)) { //assign without explicit cast var st = propertyMap.SrcType; var dt = propertyMap.DestType; if (dt.IsAssignableFrom(st) || dt.IsImplicitCastableFrom(st)) { recorder.AppendAssignment(Assign.AsNoCast, ctx); continue; } //assign with explicit cast if (dt.IsExplicitCastableFrom(st)) { recorder.AppendAssignment(Assign.AsExplicitCast, ctx); continue; } //assign with src.ToString() call if (dt == typeof(string) && st != typeof(string)) { recorder.AppendAssignment(Assign.AsToStringCall, ctx); continue; } //assign with Convert call if (st == typeof(string) && dt.IsValueType) { recorder.AppendAssignment(Assign.AsStringToValueTypeConvert, ctx); continue; } if (!st.IsValueType && !dt.IsValueType) { using (var block = new Block(recorder, "if", $"{{0}}.{ctx.SrcMemberName} == null")) { recorder.AppendLine($"{{1}}.{ctx.DestMemberName} = null;"); } using (var block = new Block(recorder, "else")) { if (st.IsCollectionType() && dt.IsCollectionType()) { string template = AssignCollections(ctx) .AddPropertyNamesToTemplate(ctx.SrcMemberName, ctx.DestMemberName); recorder.AppendLine(template); } else { string template = AssignReferenceTypes(ctx) .AddPropertyNamesToTemplate(ctx.SrcMemberName, ctx.DestMemberName); recorder.AppendLine(template); } } } else { throw new NotSupportedException(); } } } var assignment = recorder.ToAssignment(); TemplateCache.AddIfNotExist(rootMap.TypePair, assignment.RelativeTemplate); return(assignment); }