Lean  $LEAN_TAG$
ParameterAttribute.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.Reflection;
20 using QuantConnect.Logging;
21 using QuantConnect.Packets;
22 
24 {
25  /// <summary>
26  /// Specifies a field or property is a parameter that can be set
27  /// from an <see cref="AlgorithmNodePacket.Parameters"/> dictionary
28  /// </summary>
29  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
30  public class ParameterAttribute : Attribute
31  {
32  /// <summary>
33  /// Specifies the binding flags used by this implementation to resolve parameter attributes
34  /// </summary>
35  public const BindingFlags BindingFlags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance;
36 
37  /// <summary>
38  /// Gets the name of this parameter
39  /// </summary>
40  public string Name { get; private set; }
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="ParameterAttribute"/> class
44  /// </summary>
45  /// <param name="name">The name of the parameter. If null is specified
46  /// then the field or property name will be used</param>
47  public ParameterAttribute(string name = null)
48  {
49  Name = name;
50  }
51 
52  /// <summary>
53  /// Uses reflections to inspect the instance for any parameter attributes.
54  /// If a value is found in the parameters dictionary, it is set.
55  /// </summary>
56  /// <param name="parameters">The parameters dictionary</param>
57  /// <param name="instance">The instance to set parameters on</param>
58  public static void ApplyAttributes(Dictionary<string, string> parameters, object instance)
59  {
60  if (instance == null) throw new ArgumentNullException(nameof(instance));
61 
62  var type = instance.GetType();
63 
64  // get all fields/properties on the instance
65  var members = type.GetFields(BindingFlags).Concat<MemberInfo>(type.GetProperties(BindingFlags));
66  foreach (var memberInfo in members)
67  {
68  var fieldInfo = memberInfo as FieldInfo;
69  var propertyInfo = memberInfo as PropertyInfo;
70 
71  // this line make static analysis a little happier, but should never actually throw
72  if (fieldInfo == null && propertyInfo == null)
73  {
74  throw new InvalidOperationException("Resolved member that is neither FieldInfo or PropertyInfo");
75  }
76 
77  // check the member for our custom attribute
78  var attribute = memberInfo.GetCustomAttribute<ParameterAttribute>();
79  if (attribute == null) continue;
80 
81  // if no name is specified in the attribute then use the member name
82  var parameterName = attribute.Name ?? memberInfo.Name;
83 
84  // get the parameter string value to apply to the member
85  string parameterValue;
86  if (!parameters.TryGetValue(parameterName, out parameterValue)) continue;
87 
88  if (string.IsNullOrEmpty(parameterValue))
89  {
90  Log.Error($"ParameterAttribute.ApplyAttributes(): parameter '{parameterName}' provided value is null/empty, skipping");
91  continue;
92  }
93 
94  // if it's a read-only property with a parameter value we can't really do anything, bail
95  if (propertyInfo != null && !propertyInfo.CanWrite)
96  {
97  var message = $"The specified property is read only: {propertyInfo.DeclaringType}.{propertyInfo.Name}";
98  throw new InvalidOperationException(message);
99  }
100 
101  // resolve the member type
102  var memberType = fieldInfo != null ? fieldInfo.FieldType : propertyInfo.PropertyType;
103 
104  // convert the parameter string value to the member type
105  var value = parameterValue.ConvertTo(memberType);
106 
107  // set the value to the field/property
108  if (fieldInfo != null)
109  {
110  fieldInfo.SetValue(instance, value);
111  }
112  else
113  {
114  propertyInfo.SetValue(instance, value);
115  }
116  }
117  }
118 
119  /// <summary>
120  /// Resolves all parameter attributes from the specified compiled assembly path
121  /// </summary>
122  /// <param name="assembly">The assembly to inspect</param>
123  /// <returns>Parameters dictionary keyed by parameter name with a value of the member type</returns>
124  public static Dictionary<string, string> GetParametersFromAssembly(Assembly assembly)
125  {
126  var parameters = new Dictionary<string, string>();
127  foreach (var type in assembly.GetTypes())
128  {
129  foreach (var kvp in GetParametersFromType(type))
130  {
131  parameters[kvp.Key] = kvp.Value;
132  }
133  }
134  return parameters;
135  }
136 
137  /// <summary>
138  /// Resolves all parameter attributes from the specified type
139  /// </summary>
140  /// <param name="type">The type to inspect</param>
141  /// <returns>Parameters dictionary keyed by parameter name with a value of the member type</returns>
142  public static IEnumerable<KeyValuePair<string, string>> GetParametersFromType(Type type)
143  {
144  foreach (var field in type.GetFields(BindingFlags))
145  {
146  var attribute = field.GetCustomAttribute<ParameterAttribute>();
147  if (attribute != null)
148  {
149  var parameterName = attribute.Name ?? field.Name;
150  yield return new KeyValuePair<string, string>(parameterName, field.FieldType.GetBetterTypeName());
151  }
152  }
153 
154  foreach (var property in type.GetProperties(BindingFlags))
155  {
156  // ignore non-writeable properties
157  if (!property.CanWrite) continue;
158  var attribute = property.GetCustomAttribute<ParameterAttribute>();
159  if (attribute != null)
160  {
161  var parameterName = attribute.Name ?? property.Name;
162  yield return new KeyValuePair<string, string>(parameterName, property.PropertyType.GetBetterTypeName());
163  }
164  }
165  }
166  }
167 }