File: DynamicProxyMetaObject.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (402 lines) | stat: -rw-r--r-- 14,859 bytes parent folder | download
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#if !(NET35 || NET20 || WINDOWS_PHONE)
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace Newtonsoft.Json.Utilities
{
  internal sealed class DynamicProxyMetaObject<T> : DynamicMetaObject
  {
    private readonly DynamicProxy<T> _proxy;
    private readonly bool _dontFallbackFirst;

    internal DynamicProxyMetaObject(Expression expression, T value, DynamicProxy<T> proxy, bool dontFallbackFirst)
      : base(expression, BindingRestrictions.Empty, value)
    {
      _proxy = proxy;
      _dontFallbackFirst = dontFallbackFirst;
    }

    private new T Value { get { return (T)base.Value; } }

    private bool IsOverridden(string method)
    {
      return _proxy.GetType().GetMember(method, MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance).Cast<MethodInfo>()
        .Any(info =>
          // check that the method overrides the original on DynamicObjectProxy
          info.DeclaringType != typeof(DynamicProxy<T>)
          // todo - find out whether there is a way to do this in winrt
#if !NETFX_CORE
          && info.GetBaseDefinition().DeclaringType == typeof(DynamicProxy<T>)
#endif
          );
    }

    public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
    {
      return IsOverridden("TryGetMember")
           ? CallMethodWithResult("TryGetMember", binder, NoArgs, e => binder.FallbackGetMember(this, e))
           : base.BindGetMember(binder);
    }

    public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
    {
      return IsOverridden("TrySetMember")
           ? CallMethodReturnLast("TrySetMember", binder, GetArgs(value), e => binder.FallbackSetMember(this, value, e))
           : base.BindSetMember(binder, value);
    }

    public override DynamicMetaObject BindDeleteMember(DeleteMemberBinder binder)
    {
      return IsOverridden("TryDeleteMember")
           ? CallMethodNoResult("TryDeleteMember", binder, NoArgs, e => binder.FallbackDeleteMember(this, e))
           : base.BindDeleteMember(binder);
    }


    public override DynamicMetaObject BindConvert(ConvertBinder binder)
    {
      return IsOverridden("TryConvert")
           ? CallMethodWithResult("TryConvert", binder, NoArgs, e => binder.FallbackConvert(this, e))
           : base.BindConvert(binder);
    }

    public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
    {
      if (!IsOverridden("TryInvokeMember"))
        return base.BindInvokeMember(binder, args);

      //
      // Generate a tree like:
      //
      // {
      //   object result;
      //   TryInvokeMember(payload, out result)
      //      ? result
      //      : TryGetMember(payload, out result)
      //          ? FallbackInvoke(result)
      //          : fallbackResult
      // }
      //
      // Then it calls FallbackInvokeMember with this tree as the
      // "error", giving the language the option of using this
      // tree or doing .NET binding.
      //
      Fallback fallback = e => binder.FallbackInvokeMember(this, args, e);

      DynamicMetaObject call = BuildCallMethodWithResult(
          "TryInvokeMember",
          binder,
          GetArgArray(args),
          BuildCallMethodWithResult(
              "TryGetMember",
              new GetBinderAdapter(binder),
              NoArgs,
              fallback(null),
              e => binder.FallbackInvoke(e, args, null)
          ),
          null
      );

      return _dontFallbackFirst ? call : fallback(call);
    }


    public override DynamicMetaObject BindCreateInstance(CreateInstanceBinder binder, DynamicMetaObject[] args)
    {
      return IsOverridden("TryCreateInstance")
           ? CallMethodWithResult("TryCreateInstance", binder, GetArgArray(args), e => binder.FallbackCreateInstance(this, args, e))
           : base.BindCreateInstance(binder, args);
    }

    public override DynamicMetaObject BindInvoke(InvokeBinder binder, DynamicMetaObject[] args)
    {
      return IsOverridden("TryInvoke")
        ? CallMethodWithResult("TryInvoke", binder, GetArgArray(args), e => binder.FallbackInvoke(this, args, e))
        : base.BindInvoke(binder, args);
    }

    public override DynamicMetaObject BindBinaryOperation(BinaryOperationBinder binder, DynamicMetaObject arg)
    {
      return IsOverridden("TryBinaryOperation")
        ? CallMethodWithResult("TryBinaryOperation", binder, GetArgs(arg), e => binder.FallbackBinaryOperation(this, arg, e))
        : base.BindBinaryOperation(binder, arg);
    }

    public override DynamicMetaObject BindUnaryOperation(UnaryOperationBinder binder)
    {
      return IsOverridden("TryUnaryOperation")
           ? CallMethodWithResult("TryUnaryOperation", binder, NoArgs, e => binder.FallbackUnaryOperation(this, e))
           : base.BindUnaryOperation(binder);
    }

    public override DynamicMetaObject BindGetIndex(GetIndexBinder binder, DynamicMetaObject[] indexes)
    {
      return IsOverridden("TryGetIndex")
           ? CallMethodWithResult("TryGetIndex", binder, GetArgArray(indexes), e => binder.FallbackGetIndex(this, indexes, e))
           : base.BindGetIndex(binder, indexes);
    }

    public override DynamicMetaObject BindSetIndex(SetIndexBinder binder, DynamicMetaObject[] indexes, DynamicMetaObject value)
    {
      return IsOverridden("TrySetIndex")
           ? CallMethodReturnLast("TrySetIndex", binder, GetArgArray(indexes, value), e => binder.FallbackSetIndex(this, indexes, value, e))
           : base.BindSetIndex(binder, indexes, value);
    }

    public override DynamicMetaObject BindDeleteIndex(DeleteIndexBinder binder, DynamicMetaObject[] indexes)
    {
      return IsOverridden("TryDeleteIndex")
           ? CallMethodNoResult("TryDeleteIndex", binder, GetArgArray(indexes), e => binder.FallbackDeleteIndex(this, indexes, e))
           : base.BindDeleteIndex(binder, indexes);
    }

    private delegate DynamicMetaObject Fallback(DynamicMetaObject errorSuggestion);

    private readonly static Expression[] NoArgs = new Expression[0];

    private static Expression[] GetArgs(params DynamicMetaObject[] args)
    {
      return args.Select(arg => Expression.Convert(arg.Expression, typeof(object))).ToArray();
    }

    private static Expression[] GetArgArray(DynamicMetaObject[] args)
    {
      return new[] { Expression.NewArrayInit(typeof(object), GetArgs(args)) };
    }

    private static Expression[] GetArgArray(DynamicMetaObject[] args, DynamicMetaObject value)
    {
      return new Expression[]
            {
                Expression.NewArrayInit(typeof(object), GetArgs(args)),
                Expression.Convert(value.Expression, typeof(object))
            };
    }

    private static ConstantExpression Constant(DynamicMetaObjectBinder binder)
    {
      Type t = binder.GetType();
      while (!t.IsVisible())
        t = t.BaseType();
      return Expression.Constant(binder, t);
    }

    /// <summary>
    /// Helper method for generating a MetaObject which calls a
    /// specific method on Dynamic that returns a result
    /// </summary>
    private DynamicMetaObject CallMethodWithResult(string methodName, DynamicMetaObjectBinder binder, Expression[] args, Fallback fallback, Fallback fallbackInvoke = null)
    {
      //
      // First, call fallback to do default binding
      // This produces either an error or a call to a .NET member
      //
      DynamicMetaObject fallbackResult = fallback(null);

      DynamicMetaObject callDynamic = BuildCallMethodWithResult(methodName, binder, args, fallbackResult, fallbackInvoke);

      //
      // Now, call fallback again using our new MO as the error
      // When we do this, one of two things can happen:
      //   1. Binding will succeed, and it will ignore our call to
      //      the dynamic method, OR
      //   2. Binding will fail, and it will use the MO we created
      //      above.
      //

      return _dontFallbackFirst ? callDynamic : fallback(callDynamic);
    }

    private DynamicMetaObject BuildCallMethodWithResult(string methodName, DynamicMetaObjectBinder binder, Expression[] args, DynamicMetaObject fallbackResult, Fallback fallbackInvoke)
    {
      //
      // Build a new expression like:
      // {
      //   object result;
      //   TryGetMember(payload, out result) ? fallbackInvoke(result) : fallbackResult
      // }
      //
      ParameterExpression result = Expression.Parameter(typeof(object), null);

      IList<Expression> callArgs = new List<Expression>();
      callArgs.Add(Expression.Convert(Expression, typeof(T)));
      callArgs.Add(Constant(binder));
      callArgs.AddRange(args);
      callArgs.Add(result);

      DynamicMetaObject resultMetaObject = new DynamicMetaObject(result, BindingRestrictions.Empty);

      // Need to add a conversion if calling TryConvert
      if (binder.ReturnType != typeof (object))
      {
        UnaryExpression convert = Expression.Convert(resultMetaObject.Expression, binder.ReturnType);
        // will always be a cast or unbox

        resultMetaObject = new DynamicMetaObject(convert, resultMetaObject.Restrictions);
      }

      if (fallbackInvoke != null)
        resultMetaObject = fallbackInvoke(resultMetaObject);

      DynamicMetaObject callDynamic = new DynamicMetaObject(
        Expression.Block(
          new[] {result},
          Expression.Condition(
            Expression.Call(
              Expression.Constant(_proxy),
              typeof(DynamicProxy<T>).GetMethod(methodName),
              callArgs
              ),
            resultMetaObject.Expression,
            fallbackResult.Expression,
            binder.ReturnType
            )
          ),
        GetRestrictions().Merge(resultMetaObject.Restrictions).Merge(fallbackResult.Restrictions)
        );

      return callDynamic;
    }

    /// <summary>
    /// Helper method for generating a MetaObject which calls a
    /// specific method on Dynamic, but uses one of the arguments for
    /// the result.
    /// </summary>
    private DynamicMetaObject CallMethodReturnLast(string methodName, DynamicMetaObjectBinder binder, Expression[] args, Fallback fallback)
    {
      //
      // First, call fallback to do default binding
      // This produces either an error or a call to a .NET member
      //
      DynamicMetaObject fallbackResult = fallback(null);

      //
      // Build a new expression like:
      // {
      //   object result;
      //   TrySetMember(payload, result = value) ? result : fallbackResult
      // }
      //
      ParameterExpression result = Expression.Parameter(typeof(object), null);

      IList<Expression> callArgs = new List<Expression>();
      callArgs.Add(Expression.Convert(Expression, typeof (T)));
      callArgs.Add(Constant(binder));
      callArgs.AddRange(args);
      callArgs[args.Length + 1] = Expression.Assign(result, callArgs[args.Length + 1]);

      DynamicMetaObject callDynamic = new DynamicMetaObject(
          Expression.Block(
              new[] { result },
              Expression.Condition(
                  Expression.Call(
                      Expression.Constant(_proxy),
                      typeof(DynamicProxy<T>).GetMethod(methodName),
                      callArgs
                  ),
                  result,
                  fallbackResult.Expression,
                  typeof(object)
              )
          ),
          GetRestrictions().Merge(fallbackResult.Restrictions)
      );

      //
      // Now, call fallback again using our new MO as the error
      // When we do this, one of two things can happen:
      //   1. Binding will succeed, and it will ignore our call to
      //      the dynamic method, OR
      //   2. Binding will fail, and it will use the MO we created
      //      above.
      //
      return _dontFallbackFirst ? callDynamic : fallback(callDynamic);
    }

    /// <summary>
    /// Helper method for generating a MetaObject which calls a
    /// specific method on Dynamic, but uses one of the arguments for
    /// the result.
    /// </summary>
    private DynamicMetaObject CallMethodNoResult(string methodName, DynamicMetaObjectBinder binder, Expression[] args, Fallback fallback)
    {
      //
      // First, call fallback to do default binding
      // This produces either an error or a call to a .NET member
      //
      DynamicMetaObject fallbackResult = fallback(null);

      IList<Expression> callArgs = new List<Expression>();
      callArgs.Add(Expression.Convert(Expression, typeof(T)));
      callArgs.Add(Constant(binder));
      callArgs.AddRange(args);

      //
      // Build a new expression like:
      //   if (TryDeleteMember(payload)) { } else { fallbackResult }
      //
      DynamicMetaObject callDynamic = new DynamicMetaObject(
        Expression.Condition(
          Expression.Call(
            Expression.Constant(_proxy),
            typeof(DynamicProxy<T>).GetMethod(methodName),
            callArgs
            ),
          Expression.Empty(),
          fallbackResult.Expression,
          typeof (void)
          ),
        GetRestrictions().Merge(fallbackResult.Restrictions)
        );

      //
      // Now, call fallback again using our new MO as the error
      // When we do this, one of two things can happen:
      //   1. Binding will succeed, and it will ignore our call to
      //      the dynamic method, OR
      //   2. Binding will fail, and it will use the MO we created
      //      above.
      //
      return _dontFallbackFirst ? callDynamic : fallback(callDynamic);
    }

    /// <summary>
    /// Returns a Restrictions object which includes our current restrictions merged
    /// with a restriction limiting our type
    /// </summary>
    private BindingRestrictions GetRestrictions()
    {
      return (Value == null && HasValue)
           ? BindingRestrictions.GetInstanceRestriction(Expression, null)
           : BindingRestrictions.GetTypeRestriction(Expression, LimitType);
    }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
      return _proxy.GetDynamicMemberNames(Value);
    }

    // It is okay to throw NotSupported from this binder. This object
    // is only used by DynamicObject.GetMember--it is not expected to
    // (and cannot) implement binding semantics. It is just so the DO
    // can use the Name and IgnoreCase properties.
    private sealed class GetBinderAdapter : GetMemberBinder
    {
      internal GetBinderAdapter(InvokeMemberBinder binder) :
        base(binder.Name, binder.IgnoreCase)
      {
      }

      public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
      {
        throw new NotSupportedException();
      }
    }
  }
}
#endif