forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyAccessor.cs
More file actions
113 lines (94 loc) · 3.69 KB
/
PropertyAccessor.cs
File metadata and controls
113 lines (94 loc) · 3.69 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Reflection;
using ServiceStack.Text;
namespace ServiceStack.Reflection
{
public static class PropertyAccessor
{
public static Func<TEntity, object> GetPropertyFn<TEntity>(string propertyName)
{
return new PropertyAccessor<TEntity>(propertyName).GetPropertyFn();
}
//public static Func<object, object> GetPropertyFnByType(Type type, string propertyName)
//{
// var mi = typeof(PropertyAccessor).GetMethod("GetPropertyFn");
// var genericMi = mi.MakeGenericMethod(type);
// var getPropertyFn = genericMi.Invoke(null, new object[] { propertyName });
// return (Func<object, object>)getPropertyFn;
//}
public static Action<TEntity, object> SetPropertyFn<TEntity>(string propertyName)
{
return new PropertyAccessor<TEntity>(propertyName).SetPropertyFn();
}
//public static Action<object, object> SetPropertyFnByType(Type type, string propertyName)
//{
// var mi = typeof(PropertyAccessor).GetMethod("SetPropertyFn");
// var genericMi = mi.MakeGenericMethod(type);
// var setPropertyFn = genericMi.Invoke(null, new object[] { propertyName });
// return (Action<object, object>)setPropertyFn;
//}
}
public class PropertyAccessor<TEntity>
{
readonly PropertyInfo pi;
public string Name { get; set; }
public Type PropertyType { get; set; }
private readonly Func<TEntity, object> getPropertyFn;
private readonly Action<TEntity, object> setPropertyFn;
public PropertyAccessor(string propertyName)
{
this.pi = typeof(TEntity).GetPropertyInfo(propertyName);
this.Name = propertyName;
this.PropertyType = pi.PropertyType;
getPropertyFn = StaticAccessors<TEntity>.ValueUnTypedGetPropertyTypeFn(pi);
setPropertyFn = StaticAccessors<TEntity>.ValueUnTypedSetPropertyTypeFn(pi);
}
public Func<TEntity, object> GetPropertyFn()
{
return getPropertyFn;
}
public Action<TEntity, object> SetPropertyFn()
{
return setPropertyFn;
}
/// <summary>
/// Func to get the Strongly-typed field
/// </summary>
public Func<TEntity, TId> TypedGetPropertyFn<TId>()
{
return StaticAccessors<TEntity>.TypedGetPropertyFn<TId>(pi);
}
/// <summary>
/// Required to cast the return ValueType to an object for caching
/// </summary>
public Func<TEntity, object> ValueTypedGetPropertyFn<TId>()
{
return StaticAccessors<TEntity>.ValueUnTypedGetPropertyFn<TId>(pi);
}
public Func<object, object> UnTypedGetPropertyFn<TId>()
{
return StaticAccessors<TEntity>.UnTypedGetPropertyFn<TId>(pi);
}
/// <summary>
/// Func to set the Strongly-typed field
/// </summary>
public Action<TEntity, TId> TypedSetPropertyFn<TId>()
{
return StaticAccessors<TEntity>.TypedSetPropertyFn<TId>(pi);
}
/// <summary>
/// Required to cast the ValueType to an object for caching
/// </summary>
public Action<TEntity, object> ValueTypesSetPropertyFn<TId>()
{
return StaticAccessors<TEntity>.ValueUnTypedSetPropertyFn<TId>(pi);
}
/// <summary>
/// Required to cast the ValueType to an object for caching
/// </summary>
public Action<object, object> UnTypedSetPropertyFn<TId>()
{
return StaticAccessors<TEntity>.UnTypedSetPropertyFn<TId>(pi);
}
}
}