Lean  $LEAN_TAG$
GetSetPropertyDynamicMetaObject.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System.Dynamic;
17 using System.Linq.Expressions;
18 using System.Reflection;
19 
20 namespace QuantConnect.Data
21 {
22  /// <summary>
23  /// Provides an implementation of <see cref="DynamicMetaObject"/> that uses get/set methods to update
24  /// values in the dynamic object.
25  /// </summary>
26  public class GetSetPropertyDynamicMetaObject : DynamicMetaObject
27  {
28  private readonly MethodInfo _setPropertyMethodInfo;
29  private readonly MethodInfo _getPropertyMethodInfo;
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="T:QuantConnect.Data.GetSetPropertyDynamicMetaObject" /> class.
33  /// </summary>
34  /// <param name="expression">The expression representing this <see cref="T:System.Dynamic.DynamicMetaObject" /></param>
35  /// <param name="value">The value represented by the <see cref="T:System.Dynamic.DynamicMetaObject" /></param>
36  /// <param name="setPropertyMethodInfo">The set method to use for updating this dynamic object</param>
37  /// <param name="getPropertyMethodInfo">The get method to use for updating this dynamic object</param>
39  Expression expression,
40  object value,
41  MethodInfo setPropertyMethodInfo,
42  MethodInfo getPropertyMethodInfo
43  )
44  : base(expression, BindingRestrictions.Empty, value)
45  {
46  _setPropertyMethodInfo = setPropertyMethodInfo;
47  _getPropertyMethodInfo = getPropertyMethodInfo;
48  }
49 
50  /// <summary>
51  /// Performs the binding of the dynamic set member operation.
52  /// </summary>
53  /// <param name="binder">An instance of the <see cref="T:System.Dynamic.SetMemberBinder" /> that represents the details of the dynamic operation.</param>
54  /// <param name="value">The <see cref="T:System.Dynamic.DynamicMetaObject" /> representing the value for the set member operation.</param>
55  /// <returns>The new <see cref="T:System.Dynamic.DynamicMetaObject" /> representing the result of the binding.</returns>
56  public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
57  {
58  // we need to build up an expression tree that represents accessing our instance
59  var restrictions = BindingRestrictions.GetTypeRestriction(Expression, LimitType);
60 
61  var args = new Expression[]
62  {
63  // this is the name of the property to set
64  Expression.Constant(binder.Name),
65 
66  // this is the value
67  Expression.Convert(value.Expression, typeof (object))
68  };
69 
70  // set the 'this' reference
71  var self = Expression.Convert(Expression, LimitType);
72 
73  var call = Expression.Call(self, _setPropertyMethodInfo, args);
74 
75  return new DynamicMetaObject(call, restrictions);
76  }
77 
78  /// <summary>Performs the binding of the dynamic get member operation.</summary>
79  /// <param name="binder">An instance of the <see cref="T:System.Dynamic.GetMemberBinder" /> that represents the details of the dynamic operation.</param>
80  /// <returns>The new <see cref="T:System.Dynamic.DynamicMetaObject" /> representing the result of the binding.</returns>
81  public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
82  {
83  // we need to build up an expression tree that represents accessing our instance
84  var restrictions = BindingRestrictions.GetTypeRestriction(Expression, LimitType);
85 
86  // arguments for 'call'
87  var args = new Expression[]
88  {
89  // this is the name of the property to set
90  Expression.Constant(binder.Name)
91  };
92 
93  // set the 'this' reference
94  var self = Expression.Convert(Expression, LimitType);
95 
96  var call = Expression.Call(self, _getPropertyMethodInfo, args);
97 
98  return new DynamicMetaObject(call, restrictions);
99  }
100  }
101 }