Better creation in APplyTo

This commit is contained in:
honzapatCZ 2026-08-23 22:13:17 +02:00
parent 0233a26d54
commit 8a49d88153

View File

@ -665,7 +665,7 @@ namespace AutoMapProperty
return (exprSb.ToString(), projectors); return (exprSb.ToString(), projectors);
} }
public static string? GeneratePropertyTo(List<ClassToGenerate> otherClasses, string Key, ITypeSymbol FromType, ITypeSymbol ToType, bool nullable = true) public static string? GeneratePropertyTo(ClassToGenerate classToGenerate, List<ClassToGenerate> otherClasses, string Key, ITypeSymbol FromType, ITypeSymbol ToType, bool nullable = true)
{ {
@ -693,7 +693,7 @@ namespace AutoMapProperty
sb.Append(nullable ? "?" : ""); sb.Append(nullable ? "?" : "");
if (!SymbolEqualityComparer.Default.Equals(concreteToType, concreteFromType)) if (!SymbolEqualityComparer.Default.Equals(concreteToType, concreteFromType))
{ {
sb.Append(@".Select(x => (").Append(GetFullString(concreteFromType)).Append(@")x)"); sb.Append(@".Select(x => (").Append(GetFullString(concreteFromType)).Append(@")x.ApplyTo(providers, (").Append(GetFullString(concreteFromType)).Append(@")(AutoMapProperty.AutoMapPropertyUtils.Create<").Append(GetFullString(concreteFromType)).Append(",").Append(classToGenerate.SourceContainer.Identifier).Append(">(providers, source, \"").Append(Key).Append("\"))))");
sb.Append(nullable ? "?" : ""); sb.Append(nullable ? "?" : "");
} }
@ -717,14 +717,14 @@ namespace AutoMapProperty
} }
else else
{ {
// if (otherClasses.Select(x => x.Name).Contains(ToType.Name)) if (otherClasses.Select(x => x.Name).Contains(ToType.Name))
// sb.Append("this.").Append(Key).Append(".ApplyTo(providers, source.").Append(Key).Append(")"); sb.Append("this.").Append(Key).Append(".ApplyTo(providers, (").Append(GetFullString(FromType)).Append(@")(source.").Append(Key).Append(" != null ? source.").Append(Key).Append(" : AutoMapProperty.AutoMapPropertyUtils.Create<").Append(GetFullString(FromType)).Append(",").Append(classToGenerate.SourceContainer.Identifier).Append(">(providers, source, \"").Append(Key).Append("\")))");
// else else
sb.Append("(").Append(GetFullString(FromType)).Append(@")this.").Append(Key); sb.Append("(").Append(GetFullString(FromType)).Append(@")this.").Append(Key);
} }
return sb.ToString(); return sb.ToString();
} }
public static string? GenerateCorrectToProperty(List<ClassToGenerate> otherClasses, string Key, ClassToGenerate.MappableTypes Value, bool nullable = true) public static string? GenerateCorrectToProperty(ClassToGenerate classToGenerate, List<ClassToGenerate> otherClasses, string Key, ClassToGenerate.MappableTypes Value, bool nullable = true)
{ {
var retSb = new StringBuilder(); var retSb = new StringBuilder();
retSb.Append("source."); retSb.Append("source.");
@ -732,7 +732,7 @@ namespace AutoMapProperty
{ {
if (Value.FromIsReadOnly) if (Value.FromIsReadOnly)
return null; return null;
var generated = GeneratePropertyTo(otherClasses, Key, Value.FromType, Value.ToType, nullable); var generated = GeneratePropertyTo(classToGenerate, otherClasses, Key, Value.FromType, Value.ToType, nullable);
if (generated == null) if (generated == null)
return null; return null;
retSb.Append(generated); retSb.Append(generated);
@ -999,7 +999,7 @@ namespace ").Append(classToGenerate.Namespace).Append(@"
{/* {/*
if (prop.Value.FromType.IsAbstract) if (prop.Value.FromType.IsAbstract)
continue;*/ continue;*/
var property = GenerateCorrectToProperty(otherClasses, prop.Key, prop.Value); var property = GenerateCorrectToProperty(classToGenerate, otherClasses, prop.Key, prop.Value);
if (property == null) if (property == null)
continue; continue;
sb.Append(@" sb.Append(@"
@ -1022,13 +1022,12 @@ namespace ").Append(classToGenerate.Namespace).Append(@"
sb.Append(@" sb.Append(@"
public ").Append(classToGenerate.SourceContainer.Identifier).Append(@" Create(System.IServiceProvider? providers = null) public ").Append(classToGenerate.SourceContainer.Identifier).Append(@" Create(System.IServiceProvider? providers = null)
{ {
var dat = new ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(); var dat = AutoMapProperty.AutoMapPropertyUtils.Create<").Append(classToGenerate.SourceContainer.Identifier).Append(@">(providers);
return ApplyTo(providers, dat); return ApplyTo(providers, dat);
} }
public static explicit operator ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(").Append(classToGenerate.Name).Append(@" source) public static explicit operator ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(").Append(classToGenerate.Name).Append(@" source)
{ {
var dat = new ").Append(classToGenerate.SourceContainer.Identifier).Append(@"(); return source.Create(null);
return source.ApplyTo(null, dat);
}"); }");
} }
} }