~ruther/NosSmooth.Comms

ref: 2d0c91b61599398f037d97a8d6288736e01082d9 NosSmooth.Comms/src/Local/NosSmooth.Comms.Inject/Extensions/ObjectExtensions.cs -rw-r--r-- 1.1 KiB
2d0c91b6 — František Boháček chore: bump versions 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
//  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;

/// <summary>
/// Extension methods for object.
/// </summary>
public static class ObjectExtensions
{
    /// <summary>
    /// Extension for 'Object' that copies the properties to a destination object.
    /// </summary>
    /// <param name="source">The source.</param>
    /// <param name="destination">The destination.</param>
    /// <typeparam name="T">The type.</typeparam>
    public static void CopyProperties<T>(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);
        }
    }
}
Do not follow this link