-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathPatchHelper.cs
More file actions
35 lines (32 loc) · 1.09 KB
/
PatchHelper.cs
File metadata and controls
35 lines (32 loc) · 1.09 KB
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DevpartnerHelper.Functions
{
public class PatchHelper
{
private static ConcurrentDictionary<Type, PropertyInfo[]> TypePropertiesCache =
new ConcurrentDictionary<Type, PropertyInfo[]>();
public static void Patch<TPatch, TEntity>(TPatch patch, TEntity entity)
where TPatch : class
where TEntity : class
{
PropertyInfo[] properties = TypePropertiesCache.GetOrAdd(
patch.GetType(),
(type) => type.GetProperties(BindingFlags.Instance | BindingFlags.Public));
foreach (PropertyInfo prop in properties)
{
PropertyInfo orjProp = entity.GetType().GetProperty(prop.Name);
object value = prop.GetValue(patch);
if (value != null)
{
orjProp.SetValue(entity, value);
}
}
}
}
}