Last active
September 3, 2022 03:25
-
-
Save icodeintx/2c884916269117e17d51796e3df5ee37 to your computer and use it in GitHub Desktop.
Reflection Mapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ModelHelper<TSource, TDestination> where TSource : class | |
where TDestination : class | |
{ | |
public static void Map(TSource source, TDestination destination) | |
{ | |
var sourceProperties = source.GetType().GetProperties(); | |
var destinationProperties = destination.GetType().GetProperties(); | |
var destinationName = source.GetType().Name; | |
foreach (var sourceProperty in sourceProperties) | |
{ | |
foreach (var destinationProperty in destinationProperties) | |
{ | |
if (sourceProperty.Name == destinationProperty.Name && sourceProperty.PropertyType == destinationProperty.PropertyType) | |
{ | |
destinationProperty.SetValue(destination, sourceProperty.GetValue(source)); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment