24 lines
775 B
C#
24 lines
775 B
C#
namespace SchoolContracts.Extensions
|
|
{
|
|
public static class ExtensionCast
|
|
{
|
|
public static TO CastWithCommonProperties<TO, TFrom>(this TFrom fromObject)
|
|
where TO : class, new()
|
|
where TFrom : class, new()
|
|
{
|
|
TO result = new();
|
|
foreach (var propFrom in typeof(TFrom).GetProperties())
|
|
{
|
|
foreach (var propTo in typeof(TO).GetProperties())
|
|
{
|
|
if (propFrom.Name == propTo.Name && propFrom.PropertyType == propTo.PropertyType && propTo.CanWrite)
|
|
{
|
|
propTo.SetValue(result, propFrom.GetValue(fromObject));
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|