From faba5b2f7bd15b792ca485169a490594b79bf9d3 Mon Sep 17 00:00:00 2001 From: MisterOzzy Date: Wed, 11 Aug 2021 21:32:02 +0300 Subject: [PATCH] Add ability to compile all mappings and then throw AggregateException --- src/Mapster/TypeAdapterConfig.cs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Mapster/TypeAdapterConfig.cs b/src/Mapster/TypeAdapterConfig.cs index 5c1d5908..0f5770aa 100644 --- a/src/Mapster/TypeAdapterConfig.cs +++ b/src/Mapster/TypeAdapterConfig.cs @@ -575,15 +575,35 @@ private CompileArgument GetCompileArgument(TypeTuple tuple, MapType mapType, Com }; } - public void Compile() + public void Compile(bool failFast = true) { + var exceptions = new List(); var keys = RuleMap.Keys.ToList(); + foreach (var key in keys) { - if (key.Source == typeof(void)) - continue; - _mapDict[key] = Compiler(CreateMapExpression(key, MapType.Map)); - _mapToTargetDict[key] = Compiler(CreateMapExpression(key, MapType.MapToTarget)); + try + { + if (key.Source == typeof(void)) + continue; + + _mapDict[key] = Compiler(CreateMapExpression(key, MapType.Map)); + _mapToTargetDict[key] = Compiler(CreateMapExpression(key, MapType.MapToTarget)); + } + catch (Exception ex) + { + if (failFast) + { + throw; + } + + exceptions.Add(ex); + } + } + + if (exceptions.Count > 0) + { + throw new AggregateException(exceptions); } }