Repository navigation
Expand file tree
/
Copy pathTypeReflectionCache.cs
More file actions
48 lines (39 loc) · 1.9 KB
/
Copy pathTypeReflectionCache.cs
File metadata and controls
48 lines (39 loc) · 1.9 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
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Collections.Concurrent;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
public static class TypeReflectionCache
{
private static readonly ConcurrentDictionary<Type, Delegate> CompiledExpressions = new();
private static readonly ConcurrentDictionary<Type, List<(string name, Type type)>> PropertyDetails = new();
public static Func<T, object[]> GetOrCreateObjectArrayConverter<T>()
{
if (CompiledExpressions.TryGetValue(typeof(T), out var del))
return del as Func<T, object[]>;
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var parameterExpression = Expression.Parameter(typeof(T), "entity");
var newArrayExpression = Expression.NewArrayInit(
typeof(object),
properties.Select(p => Expression.Convert(Expression.Property(parameterExpression, p), typeof(object))).ToArray()
);
var lambda = Expression.Lambda<Func<T, object[]>>(newArrayExpression, parameterExpression).Compile();
CompiledExpressions.TryAdd(typeof(T), lambda);
return lambda;
}
public static List<(string name, Type type)> GetOrCreatePropertyDetails<T>()
{
if (PropertyDetails.TryGetValue(typeof(T), out var details))
return details;
var propDetails = new List<(string name, Type type)>();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead)
.ToList();
// Create columns from property names
foreach (var prop in properties)
{
propDetails.Add((prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType));
}
PropertyDetails.TryAdd(typeof(T), propDetails);
return propDetails;
}
}