// // ObjectExtensions.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Diagnostics; using System.Reflection; namespace NosSmooth.Comms.Inject.Extensions; /// /// Extension methods for object. /// public static class ObjectExtensions { /// /// Extension for 'Object' that copies the properties to a destination object. /// /// The source. /// The destination. /// The type. public static void CopyProperties(this T? source, T destination) where T : notnull { if (source is null) { return; } var properties = source.GetType().GetProperties(); foreach (var p in properties.Where(prop => prop.CanRead && prop.CanWrite)) { object? copyValue = p.GetValue(source); p.SetValue(destination, copyValue); } } }