Skip to content

Commit

Permalink
T #98 used dictionary for mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Nov 1, 2022
1 parent be4904d commit 07ca81d
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@ 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));
}

return result;
}

public static object? GetMappedValue(Type newValue, object inputValue)
public static object? GetMappedValue(Type newValue, object? inputValue)
{
if (inputValue is null) return default;

Expand Down

0 comments on commit 07ca81d

Please sign in to comment.