forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeReflector.cs
More file actions
94 lines (80 loc) · 2.85 KB
/
TypeReflector.cs
File metadata and controls
94 lines (80 loc) · 2.85 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Reflection;
using ServiceStack.Reflection;
using ServiceStack.Text;
namespace ServiceStack
{
public static class TypeReflector<T>
{
public static readonly Dictionary<string, Func<object, object>> PublicGetters =
new Dictionary<string, Func<object, object>>(PclExport.Instance.InvariantComparerIgnoreCase);
public static readonly Dictionary<string, Action<object, object>> PublicSetters =
new Dictionary<string, Action<object, object>>(PclExport.Instance.InvariantComparerIgnoreCase);
public static readonly Dictionary<string, PropertyInfo> PublicProperties =
new Dictionary<string, PropertyInfo>(PclExport.Instance.InvariantComparerIgnoreCase);
public static readonly PropertyInfo[] PublicPropertyInfos;
static TypeReflector()
{
PublicPropertyInfos = typeof(T).GetPublicProperties();
foreach (var pi in PublicPropertyInfos)
{
try
{
PublicGetters[pi.Name] = pi.GetValueGetter(typeof(T));
PublicSetters[pi.Name] = pi.GetValueSetter(typeof(T));
PublicProperties[pi.Name] = pi;
}
catch (Exception ex)
{
Tracer.Instance.WriteError(ex);
}
}
}
public static PropertyInfo GetPublicProperty(string name)
{
foreach (var pi in PublicPropertyInfos)
{
if (pi.Name == name)
return pi;
}
return null;
}
public static Func<object, object> GetPublicGetter(PropertyInfo pi)
{
if (pi == null)
return null;
Func<object, object> fn;
return PublicGetters.TryGetValue(pi.Name, out fn)
? fn
: pi.GetValueGetter();
}
public static Func<object, object> GetPublicGetter(string name)
{
if (name == null)
return null;
Func<object, object> fn;
return PublicGetters.TryGetValue(name, out fn)
? fn
: null;
}
public static Action<object, object> GetPublicSetter(PropertyInfo pi)
{
if (pi == null)
return null;
Action<object, object> fn;
return PublicSetters.TryGetValue(pi.Name, out fn)
? fn
: pi.GetValueSetter();
}
public static Action<object, object> GetPublicSetter(string name)
{
if (name == null)
return null;
Action<object, object> fn;
return PublicSetters.TryGetValue(name, out fn)
? fn
: null;
}
}
}