-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRowStoreMetaObject.cs
66 lines (56 loc) · 2.34 KB
/
RowStoreMetaObject.cs
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
using System;
using System.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
namespace SecurityDriven.TinyORM
{
// based on Dapper's approach
sealed class RowStoreMetaObject : DynamicMetaObject
{
static PropertyInfo s_rowStoreIndexerPropertyInfo = typeof(RowStore).GetProperty("Item", new Type[] { typeof(string) });
static MethodInfo s_getValueMethod = s_rowStoreIndexerPropertyInfo.GetGetMethod(nonPublic: false);
static MethodInfo s_setValueMethod = s_rowStoreIndexerPropertyInfo.GetSetMethod(nonPublic: false);
public RowStoreMetaObject(Expression expression, BindingRestrictions restrictions) : base(expression, restrictions) { }
public RowStoreMetaObject(Expression expression, BindingRestrictions restrictions, object value) : base(expression, restrictions, value) { }
public sealed override DynamicMetaObject BindGetMember(GetMemberBinder binder)
{
var parameters = new Expression[] { Expression.Constant(binder.Name) };
var callMethod = new DynamicMetaObject(
Expression.Call(
Expression.Convert(Expression, LimitType),
s_getValueMethod,
parameters),
BindingRestrictions.GetTypeRestriction(Expression, LimitType)
);
return callMethod;
}// BindGetMember()
// Needed for Visual basic dynamic support
public sealed override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
{
var parameters = new Expression[] { Expression.Constant(binder.Name) };
var callMethod = new DynamicMetaObject(
Expression.Call(
Expression.Convert(Expression, LimitType),
s_getValueMethod,
parameters),
BindingRestrictions.GetTypeRestriction(Expression, LimitType)
);
return callMethod;
}// BindInvokeMember()
public sealed override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
{
var objectType = typeof(object);
var parameters = new Expression[] { Expression.Constant(binder.Name), Expression.Convert(value.Expression, objectType) };
var callMethod = new DynamicMetaObject(
Expression.Block(
Expression.Call(
Expression.Convert(Expression, LimitType),
s_setValueMethod,
parameters
), Expression.Default(objectType)),
BindingRestrictions.GetTypeRestriction(Expression, LimitType)
);
return callMethod;
}// BindSetmember()
}// class RowStoreMetaObject
}//ns