Skip to content

Instantly share code, notes, and snippets.

@icodeintx
Last active September 3, 2022 03:25
Show Gist options
  • Save icodeintx/2c884916269117e17d51796e3df5ee37 to your computer and use it in GitHub Desktop.
Save icodeintx/2c884916269117e17d51796e3df5ee37 to your computer and use it in GitHub Desktop.
Reflection Mapper
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