namespace EonaCat.gRPC.Api.Helpers;
public static class CustomMapper
{
///
/// Proto to Entity/DTO And Reverse
///
///
///
///
/// TDestination
public static TDestination Map(TSource src) where TDestination : new()
{
try
{
var tDest = (TDestination)Activator.CreateInstance(typeof(TDestination))!;
if (src == null)
return tDest;
var srcClassType = src.GetType();
var srcProperties = srcClassType.GetProperties();
foreach (var srcProperty in srcProperties)
{
var destPropertyInfo = tDest.GetType().GetProperty(srcProperty.Name);
if (srcProperty.GetType() == destPropertyInfo?.GetType())
destPropertyInfo.SetValue(tDest, srcProperty.GetValue(src, null), null);
}
return tDest;
}
catch (Exception e)
{
throw new Exception($"Unsupported mapping.\n{e.Message}");
}
}
}