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