Lean  $LEAN_TAG$
ObjectActivator.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;
17 using System.Collections.Generic;
18 using System.Linq;
19 using System.Linq.Expressions;
20 using CloneExtensions;
21 using Fasterflect;
22 using QuantConnect.Logging;
23 
24 namespace QuantConnect.Util
25 {
26  /// <summary>
27  /// Provides methods for creating new instances of objects
28  /// </summary>
29  public static class ObjectActivator
30  {
31  private static readonly object _lock = new object();
32  private static readonly object[] _emptyObjectArray = new object[0];
33  private static readonly Dictionary<Type, MethodInvoker> _cloneMethodsByType = new Dictionary<Type, MethodInvoker>();
34  private static readonly Dictionary<Type, Func<object[], object>> _activatorsByType = new Dictionary<Type, Func<object[], object>>();
35 
36  static ObjectActivator()
37  {
38  // we can reuse the symbol instance in the clone since it's immutable
39  ((HashSet<Type>) CloneFactory.KnownImmutableTypes).Add(typeof (Symbol));
40  ((HashSet<Type>) CloneFactory.KnownImmutableTypes).Add(typeof (SecurityIdentifier));
41  }
42 
43  /// <summary>
44  /// Fast Object Creator from Generic Type:
45  /// Modified from http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/
46  /// </summary>
47  /// <remarks>This assumes that the type has a parameterless, default constructor</remarks>
48  /// <param name="dataType">Type of the object we wish to create</param>
49  /// <returns>Method to return an instance of object</returns>
50  public static Func<object[], object> GetActivator(Type dataType)
51  {
52  lock (_lock)
53  {
54  // if we already have it, just use it
55  Func<object[], object> factory;
56  if (_activatorsByType.TryGetValue(dataType, out factory))
57  {
58  return factory;
59  }
60 
61  var ctor = dataType.GetConstructor(new Type[] {});
62 
63  //User has forgotten to include a parameterless constructor:
64  if (ctor == null) return null;
65 
66  var paramsInfo = ctor.GetParameters();
67 
68  //create a single param of type object[]
69  var param = Expression.Parameter(typeof (object[]), "args");
70  var argsExp = new Expression[paramsInfo.Length];
71 
72  for (var i = 0; i < paramsInfo.Length; i++)
73  {
74  var index = Expression.Constant(i);
75  var paramType = paramsInfo[i].ParameterType;
76  var paramAccessorExp = Expression.ArrayIndex(param, index);
77  var paramCastExp = Expression.Convert(paramAccessorExp, paramType);
78  argsExp[i] = paramCastExp;
79  }
80 
81  var newExp = Expression.New(ctor, argsExp);
82  var lambda = Expression.Lambda(typeof (Func<object[], object>), newExp, param);
83  factory = (Func<object[], object>) lambda.Compile();
84 
85  // save it for later
86  _activatorsByType.Add(dataType, factory);
87 
88  return factory;
89  }
90  }
91 
92  /// <summary>
93  /// Clones the specified instance using reflection
94  /// </summary>
95  /// <param name="instanceToClone">The instance to be cloned</param>
96  /// <returns>A field/property wise, non-recursive clone of the instance</returns>
97  public static object Clone(object instanceToClone)
98  {
99  var type = instanceToClone.GetType();
100  MethodInvoker func;
101  if (_cloneMethodsByType.TryGetValue(type, out func))
102  {
103  return func(null, instanceToClone);
104  }
105 
106  // public static T GetClone<T>(this T source, CloningFlags flags)
107  var method = typeof (CloneFactory).GetMethods().FirstOrDefault(x => x.Name == "GetClone" && x.GetParameters().Length == 1);
108  method = method.MakeGenericMethod(type);
109  func = method.DelegateForCallMethod();
110  _cloneMethodsByType[type] = func;
111  return func(null, instanceToClone);
112  }
113 
114  /// <summary>
115  /// Clones the specified instance and then casts it to T before returning
116  /// </summary>
117  public static T Clone<T>(T instanceToClone) where T : class
118  {
119  var clone = Clone((object)instanceToClone) as T;
120  if (clone == null)
121  {
122  throw new ArgumentException($"Unable to clone instance of type {instanceToClone.GetType().Name} to {typeof(T).Name}");
123  }
124  return clone;
125  }
126 
127  /// <summary>
128  /// Adds method to return an instance of object
129  /// </summary>
130  /// <param name="key">The key of the method to add</param>
131  /// <param name="value">The value of the method to add</param>
132  public static void AddActivator(Type key, Func<object[], object> value)
133  {
134  if (!_activatorsByType.ContainsKey(key))
135  {
136  _activatorsByType.Add(key, value);
137  }
138  else
139  {
140  throw new ArgumentException($"ObjectActivator.AddActivator(): a method to return an instance of {key.Name} has already been added");
141  }
142  }
143 
144  /// <summary>
145  /// Reset the object activators
146  /// </summary>
147  public static void ResetActivators()
148  {
149  _activatorsByType.Clear();
150  }
151  }
152 }