namespace SchoolContracts.Extensions { public static class ExtensionCast { public static TO CastWithCommonProperties(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; } } }