-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrototypalObject.cs
307 lines (265 loc) · 10.9 KB
/
PrototypalObject.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#region License
//
// Copyright (c) 2012, Ian Davis
//
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
// See the file LICENSE.txt for details.
//
#endregion
#region Using Directives
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
#endregion
namespace Archetype.Sandbox
{
internal delegate bool TryBinaryOperationMissing( BinaryOperationBinder binder, object arg, out object result );
internal delegate bool TryConvertMissing( ConvertBinder binder, out object result );
internal delegate bool TryCreateInstanceMissing( CreateInstanceBinder binder, object[] args, out object result );
internal delegate bool TryDeleteIndexMissing( DeleteIndexBinder binder, object[] indexes );
internal delegate bool TryDeleteMemberMissing( DeleteMemberBinder binder );
internal delegate bool TryGetIndexMissing( GetIndexBinder binder, object[] indexes, out object result );
internal delegate bool TryGetMemberMissing( GetMemberBinder binder, out object result );
internal delegate bool TryInvokeMemberMissing( InvokeMemberBinder binder, object[] args, out object result );
internal delegate bool TryInvokeMissing( InvokeBinder binder, object[] args, out object result );
internal delegate bool TrySetIndexMissing( SetIndexBinder binder, object[] indexes, object value );
internal delegate bool TrySetMemberMissing( SetMemberBinder binder, object value );
internal delegate bool TryUnaryOperationMissing( UnaryOperationBinder binder, out object result );
internal class PrototypalObject : DelegatingObject
{
private const BindingFlags DefaultBindingFlags =
BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.Public;
public PrototypalObject()
: this( null )
{
}
public PrototypalObject( object prototype )
: base( prototype )
{
}
public virtual TryBinaryOperationMissing TryBinaryOperationMissing { get; set; }
public virtual TryConvertMissing TryConvertMissing { get; set; }
public virtual TryCreateInstanceMissing TryCreateInstanceMissing { get; set; }
public virtual TryDeleteIndexMissing TryDeleteIndexMissing { get; set; }
public virtual TryDeleteMemberMissing TryDeleteMemberMissing { get; set; }
public virtual TryGetIndexMissing TryGetIndexMissing { get; set; }
public virtual TryGetMemberMissing TryGetMemberMissing { get; set; }
public virtual TryInvokeMemberMissing TryInvokeMemberMissing { get; set; }
public virtual TryInvokeMissing TryInvokeMissing { get; set; }
public virtual TrySetIndexMissing TrySetIndexMissing { get; set; }
public virtual TrySetMemberMissing TrySetMemberMissing { get; set; }
public virtual TryUnaryOperationMissing TryUnaryOperationMissing { get; set; }
public override bool TryBinaryOperation( BinaryOperationBinder binder, object arg, out object result )
{
if ( TryBinaryOperationMissing == null )
{
result = null;
return false;
}
return TryBinaryOperationMissing( binder, arg, out result );
}
public override bool TryConvert( ConvertBinder binder, out object result )
{
if ( TryConvertMissing == null )
{
result = null;
return false;
}
return TryConvertMissing( binder, out result );
}
public override bool TryCreateInstance( CreateInstanceBinder binder, object[] args, out object result )
{
if ( TryCreateInstanceMissing == null )
{
result = null;
return false;
}
return TryCreateInstanceMissing( binder, args, out result );
}
public override bool TryDeleteIndex( DeleteIndexBinder binder, object[] indexes )
{
if ( TryDeleteIndexMissing == null )
{
return true;
}
return TryDeleteIndexMissing( binder, indexes );
}
public override bool TryDeleteMember( DeleteMemberBinder binder )
{
if ( TryDeleteMemberMissing == null )
{
return true;
}
return TryDeleteMemberMissing( binder );
}
public override bool TryGetIndex( GetIndexBinder binder, object[] indexes, out object result )
{
if ( TryGetIndexMissing == null )
{
result = null;
return false;
}
return TryGetIndexMissing( binder, indexes, out result );
}
public override bool TryGetMember( GetMemberBinder binder, out object result )
{
if ( TryGetStaticMember( binder, out result ) )
{
return true;
}
if ( TryGetMemberMissing == null )
{
return false;
}
return TryGetMemberMissing( binder, out result );
}
public override bool TryInvokeMember( InvokeMemberBinder binder, object[] args, out object result )
{
if ( TryInvokeStaticMember( binder, args, out result ) )
{
return true;
}
if ( TryInvokeMemberMissing == null )
{
return false;
}
return TryInvokeMemberMissing( binder, args, out result );
}
public override bool TryInvoke( InvokeBinder binder, object[] args, out object result )
{
if ( TryInvokeMissing == null )
{
result = null;
return false;
}
return TryInvokeMissing( binder, args, out result );
}
public override bool TrySetIndex( SetIndexBinder binder, object[] indexes, object value )
{
if ( TrySetIndexMissing == null )
{
return false;
}
return TrySetIndexMissing( binder, indexes, value );
}
public override bool TrySetMember( SetMemberBinder binder, object value )
{
if ( TrySetStaticMember( binder, value ) )
{
return true;
}
if ( TrySetMemberMissing == null )
{
return false;
}
return TrySetMemberMissing( binder, value );
}
public override bool TryUnaryOperation( UnaryOperationBinder binder, out object result )
{
if ( TryUnaryOperationMissing == null )
{
result = null;
return false;
}
return TryUnaryOperationMissing( binder, out result );
}
protected virtual bool TryInvokeStaticMember( InvokeMemberBinder binder, object[] args, out object result )
{
MethodInfo method = GetType().GetMethod( binder.Name, DefaultBindingFlags );
if ( method != null )
{
result = method.Invoke( null, args );
return true;
}
foreach ( DynamicObject prototype in Modules.OfType<DynamicObject>() )
{
if ( prototype.TryInvokeMember( binder, args, out result ) )
{
return true;
}
}
result = null;
return false;
}
protected virtual bool TryGetStaticMember( GetMemberBinder binder, out object result )
{
PropertyInfo property = GetType().GetProperty( binder.Name, DefaultBindingFlags );
if ( property != null )
{
result = property.GetValue( null, null );
return true;
}
FieldInfo field = GetType().GetField( binder.Name, DefaultBindingFlags );
if ( field != null )
{
result = field.GetValue( null );
return true;
}
foreach ( DynamicObject prototype in Modules.OfType<DynamicObject>() )
{
if ( prototype.TryGetMember( binder, out result ) )
{
return true;
}
}
result = null;
return false;
}
protected virtual bool TrySetStaticMember( SetMemberBinder binder, object value )
{
PropertyInfo property = GetType().GetProperty( binder.Name, DefaultBindingFlags );
if ( property != null )
{
property.SetValue( null, value, null );
return true;
}
FieldInfo field = GetType().GetField( binder.Name, DefaultBindingFlags );
if ( field != null )
{
field.SetValue( null, value );
return true;
}
return Modules.OfType<DynamicObject>().Any( prototype => prototype.TrySetMember( binder, value ) );
}
public static PrototypalObject AsPrototypalObject( IDynamicMetaObjectProvider prototype )
{
if ( prototype == null )
{
return new PrototypalObject();
}
return prototype as PrototypalObject ?? new PrototypalObject( prototype );
}
public virtual bool RespondsTo( string name )
{
return RespondsTo( name, this );
}
public virtual bool RespondsTo( string name, object target )
{
var provider = target as IDynamicMetaObjectProvider;
IEnumerable<string> dynamicMembers = new string[0];
if ( provider != null )
{
DynamicMetaObject meta = provider.GetMetaObject( Expression.Constant( target ) );
dynamicMembers = meta.GetDynamicMemberNames();
}
Type type = target.GetType();
IEnumerable<string> members = type.GetMembers().Select( member => member.Name );
members = members.Union( dynamicMembers );
bool respondsTo = members.Any( item => String.Equals( item, name, StringComparison.OrdinalIgnoreCase ) );
if ( respondsTo )
{
return true;
}
var prototypalObject = target as PrototypalObject;
if ( prototypalObject != null &&
prototypalObject.Modules != null )
{
return prototypalObject.Modules.Any( module => RespondsTo( name, module ) );
}
return false;
}
}
}