~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/DynamicUtils.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Dynamic;
 
30
using System.Linq;
 
31
using System.Linq.Expressions;
 
32
using System.Reflection;
 
33
using System.Runtime.CompilerServices;
 
34
using System.Text;
 
35
using System.Globalization;
 
36
using Newtonsoft.Json.Serialization;
 
37
 
 
38
namespace Newtonsoft.Json.Utilities
 
39
{
 
40
  internal static class DynamicUtils
 
41
  {
 
42
    internal static class BinderWrapper
 
43
    {
 
44
#if !SILVERLIGHT
 
45
      public const string CSharpAssemblyName = "Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
 
46
#else
 
47
      public const string CSharpAssemblyName = "Microsoft.CSharp, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
 
48
#endif
 
49
 
 
50
      private const string BinderTypeName = "Microsoft.CSharp.RuntimeBinder.Binder, " + CSharpAssemblyName;
 
51
      private const string CSharpArgumentInfoTypeName = "Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo, " + CSharpAssemblyName;
 
52
      private const string CSharpArgumentInfoFlagsTypeName = "Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, " + CSharpAssemblyName;
 
53
      private const string CSharpBinderFlagsTypeName = "Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, " + CSharpAssemblyName;
 
54
 
 
55
      private static object _getCSharpArgumentInfoArray;
 
56
      private static object _setCSharpArgumentInfoArray;
 
57
      private static MethodCall<object, object> _getMemberCall;
 
58
      private static MethodCall<object, object> _setMemberCall;
 
59
      private static bool _init;
 
60
 
 
61
      private static void Init()
 
62
      {
 
63
        if (!_init)
 
64
        {
 
65
          Type binderType = Type.GetType(BinderTypeName, false);
 
66
          if (binderType == null)
 
67
            throw new InvalidOperationException("Could not resolve type '{0}'. You may need to add a reference to Microsoft.CSharp.dll to work with dynamic types.".FormatWith(CultureInfo.InvariantCulture, BinderTypeName));
 
68
 
 
69
          // None
 
70
          _getCSharpArgumentInfoArray = CreateSharpArgumentInfoArray(0);
 
71
          // None, Constant | UseCompileTimeType
 
72
          _setCSharpArgumentInfoArray = CreateSharpArgumentInfoArray(0, 3);
 
73
          CreateMemberCalls();
 
74
 
 
75
          _init = true;
 
76
        }
 
77
      }
 
78
 
 
79
      private static object CreateSharpArgumentInfoArray(params int[] values)
 
80
      {
 
81
        Type csharpArgumentInfoType = Type.GetType(CSharpArgumentInfoTypeName);
 
82
        Type csharpArgumentInfoFlags = Type.GetType(CSharpArgumentInfoFlagsTypeName);
 
83
 
 
84
        Array a = Array.CreateInstance(csharpArgumentInfoType, values.Length);
 
85
 
 
86
        for (int i = 0; i < values.Length; i++)
 
87
        {
 
88
          MethodInfo createArgumentInfoMethod = csharpArgumentInfoType.GetMethod("Create", BindingFlags.Public | BindingFlags.Static, null, new[] { csharpArgumentInfoFlags, typeof(string) }, null);
 
89
          object arg = createArgumentInfoMethod.Invoke(null, new object[] { 0, null });
 
90
          a.SetValue(arg, i);
 
91
        }
 
92
 
 
93
        return a;
 
94
      }
 
95
 
 
96
      private static void CreateMemberCalls()
 
97
      {
 
98
        Type csharpArgumentInfoType = Type.GetType(CSharpArgumentInfoTypeName);
 
99
        Type csharpBinderFlagsType = Type.GetType(CSharpBinderFlagsTypeName);
 
100
        Type binderType = Type.GetType(BinderTypeName);
 
101
 
 
102
        Type csharpArgumentInfoTypeEnumerableType = typeof(IEnumerable<>).MakeGenericType(csharpArgumentInfoType);
 
103
 
 
104
        MethodInfo getMemberMethod = binderType.GetMethod("GetMember", BindingFlags.Public | BindingFlags.Static, null, new[] { csharpBinderFlagsType, typeof(string), typeof(Type), csharpArgumentInfoTypeEnumerableType }, null);
 
105
        _getMemberCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(getMemberMethod);
 
106
 
 
107
        MethodInfo setMemberMethod = binderType.GetMethod("SetMember", BindingFlags.Public | BindingFlags.Static, null, new[] { csharpBinderFlagsType, typeof(string), typeof(Type), csharpArgumentInfoTypeEnumerableType }, null);
 
108
        _setMemberCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(setMemberMethod);
 
109
      }
 
110
 
 
111
      public static CallSiteBinder GetMember(string name, Type context)
 
112
      {
 
113
        Init();
 
114
        return (CallSiteBinder)_getMemberCall(null, 0, name, context, _getCSharpArgumentInfoArray);
 
115
      }
 
116
 
 
117
      public static CallSiteBinder SetMember(string name, Type context)
 
118
      {
 
119
        Init();
 
120
        return (CallSiteBinder)_setMemberCall(null, 0, name, context, _setCSharpArgumentInfoArray);
 
121
      }
 
122
    }
 
123
 
 
124
    public static bool TryGetMember(this IDynamicMetaObjectProvider dynamicProvider, string name, out object value)
 
125
    {
 
126
      ValidationUtils.ArgumentNotNull(dynamicProvider, "dynamicProvider");
 
127
 
 
128
      GetMemberBinder getMemberBinder = (GetMemberBinder) BinderWrapper.GetMember(name, typeof (DynamicUtils));
 
129
 
 
130
      CallSite<Func<CallSite, object, object>> callSite = CallSite<Func<CallSite, object, object>>.Create(new NoThrowGetBinderMember(getMemberBinder));
 
131
 
 
132
      object result = callSite.Target(callSite, dynamicProvider);
 
133
 
 
134
      if (!ReferenceEquals(result, NoThrowExpressionVisitor.ErrorResult))
 
135
      {
 
136
        value = result;
 
137
        return true;
 
138
      }
 
139
      else
 
140
      {
 
141
        value = null;
 
142
        return false;
 
143
      }
 
144
    }
 
145
 
 
146
    public static bool TrySetMember(this IDynamicMetaObjectProvider dynamicProvider, string name, object value)
 
147
    {
 
148
      ValidationUtils.ArgumentNotNull(dynamicProvider, "dynamicProvider");
 
149
 
 
150
      SetMemberBinder binder = (SetMemberBinder)BinderWrapper.SetMember(name, typeof(DynamicUtils));
 
151
 
 
152
      var setterSite = CallSite<Func<CallSite, object, object, object>>.Create(new NoThrowSetBinderMember(binder));
 
153
 
 
154
      object result = setterSite.Target(setterSite, dynamicProvider, value);
 
155
 
 
156
      return !ReferenceEquals(result, NoThrowExpressionVisitor.ErrorResult);
 
157
    }
 
158
 
 
159
    public static IEnumerable<string> GetDynamicMemberNames(this IDynamicMetaObjectProvider dynamicProvider)
 
160
    {
 
161
      DynamicMetaObject metaObject = dynamicProvider.GetMetaObject(Expression.Constant(dynamicProvider));
 
162
      return metaObject.GetDynamicMemberNames();
 
163
    }
 
164
 
 
165
    internal class NoThrowGetBinderMember : GetMemberBinder
 
166
    {
 
167
      private readonly GetMemberBinder _innerBinder;
 
168
 
 
169
      public NoThrowGetBinderMember(GetMemberBinder innerBinder)
 
170
        : base(innerBinder.Name, innerBinder.IgnoreCase)
 
171
      {
 
172
        _innerBinder = innerBinder;
 
173
      }
 
174
 
 
175
      public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
 
176
      {
 
177
        DynamicMetaObject retMetaObject = _innerBinder.Bind(target, new DynamicMetaObject[] { });
 
178
 
 
179
        NoThrowExpressionVisitor noThrowVisitor = new NoThrowExpressionVisitor();
 
180
        Expression resultExpression = noThrowVisitor.Visit(retMetaObject.Expression);
 
181
 
 
182
        DynamicMetaObject finalMetaObject = new DynamicMetaObject(resultExpression, retMetaObject.Restrictions);
 
183
        return finalMetaObject;
 
184
      }
 
185
    }
 
186
 
 
187
    internal class NoThrowSetBinderMember : SetMemberBinder
 
188
    {
 
189
      private readonly SetMemberBinder _innerBinder;
 
190
 
 
191
      public NoThrowSetBinderMember(SetMemberBinder innerBinder)
 
192
        : base(innerBinder.Name, innerBinder.IgnoreCase)
 
193
      {
 
194
        _innerBinder = innerBinder;
 
195
      }
 
196
 
 
197
      public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
 
198
      {
 
199
        DynamicMetaObject retMetaObject = _innerBinder.Bind(target, new DynamicMetaObject[] { value });
 
200
 
 
201
        NoThrowExpressionVisitor noThrowVisitor = new NoThrowExpressionVisitor();
 
202
        Expression resultExpression = noThrowVisitor.Visit(retMetaObject.Expression);
 
203
 
 
204
        DynamicMetaObject finalMetaObject = new DynamicMetaObject(resultExpression, retMetaObject.Restrictions);
 
205
        return finalMetaObject;
 
206
      }
 
207
    }
 
208
 
 
209
 
 
210
    internal class NoThrowExpressionVisitor : ExpressionVisitor
 
211
    {
 
212
      internal static readonly object ErrorResult = new object();
 
213
 
 
214
      protected override Expression VisitConditional(ConditionalExpression node)
 
215
      {
 
216
        // if the result of a test is to throw an error, rewrite to result an error result value
 
217
        if (node.IfFalse.NodeType == ExpressionType.Throw)
 
218
          return Expression.Condition(node.Test, node.IfTrue, Expression.Constant(ErrorResult));
 
219
 
 
220
        return base.VisitConditional(node);
 
221
      }
 
222
    }
 
223
  }
 
224
}
 
225
#endif
 
 
b'\\ No newline at end of file'