From 07ca81df5090ffac6cc71aa7347ca4ee41acc8f9 Mon Sep 17 00:00:00 2001 From: huebyte Date: Tue, 1 Nov 2022 01:22:52 +0100 Subject: [PATCH] T #98 used dictionary for mapping --- .../Utilities/ReflectionMapper.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs b/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs index e87822d..d08cc68 100644 --- a/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs +++ b/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs @@ -15,17 +15,15 @@ public static class ReflectionMapper T result = new(); var tProps = result.GetType().GetProperties(); - var inputProps = input.GetType().GetProperties(); + var inputProps = input.GetType().GetProperties().ToDictionary(e => e.Name, e => e); foreach (var prop in tProps) { - var matchingProp = inputProps.FirstOrDefault(iprop => iprop.Name == prop.Name); - if (matchingProp is null) - continue; - - var inputPropInstance = matchingProp.GetValue(input, null); + inputProps.TryGetValue(prop.Name, out PropertyInfo? matchingProp); + + if (matchingProp is null) continue; - Console.WriteLine($"Setting {prop.PropertyType} from {inputPropInstance?.GetType()}"); + var inputPropInstance = matchingProp.GetValue(input, null); prop.SetValue(result, GetMappedValue(prop.PropertyType, inputPropInstance)); } @@ -33,7 +31,7 @@ public static class ReflectionMapper return result; } - public static object? GetMappedValue(Type newValue, object inputValue) + public static object? GetMappedValue(Type newValue, object? inputValue) { if (inputValue is null) return default;