Lean  $LEAN_TAG$
DocumentationAttribute.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.Linq;
18 using Newtonsoft.Json;
19 using System.Reflection;
20 
21 namespace QuantConnect
22 {
23  /// <summary>
24  /// Custom attribute used for documentation
25  /// </summary>
26  [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
27  [DocumentationAttribute("Reference")]
28  public sealed class DocumentationAttribute : Attribute
29  {
30  private static readonly DocumentationAttribute Attribute =
31  typeof(DocumentationAttribute).GetCustomAttributes<DocumentationAttribute>().Single();
32  private static readonly string BasePath =
33  Attribute.FileName.Substring(0, Attribute.FileName.LastIndexOf("Common", StringComparison.Ordinal));
34 
35  /// <summary>
36  /// The documentation tag
37  /// </summary>
38  [JsonProperty(PropertyName = "tag")]
39  public string Tag { get; }
40 
41  /// <summary>
42  /// The associated weight of this attribute and tag
43  /// </summary>
44  [JsonProperty(PropertyName = "weight")]
45  public int Weight { get; }
46 
47  /// <summary>
48  /// The associated line of this attribute
49  /// </summary>
50  [JsonProperty(PropertyName = "line")]
51  public int Line { get; }
52 
53  /// <summary>
54  /// The associated file name of this attribute
55  /// </summary>
56  [JsonProperty(PropertyName = "fileName")]
57  public string FileName { get; }
58 
59  /// <summary>
60  /// The attributes type id, we override it to ignore it when serializing
61  /// </summary>
62  [JsonIgnore]
63  public override object TypeId => base.TypeId;
64 
65  /// <summary>
66  /// Creates a new instance
67  /// </summary>
68  public DocumentationAttribute(string tag, int weight = 0,
69  [System.Runtime.CompilerServices.CallerLineNumber] int line = 0,
70  [System.Runtime.CompilerServices.CallerFilePath] string fileName = "")
71  {
72  Tag = tag;
73  Line = line;
74  Weight = weight;
75  // will be null for the attribute of DocumentationAttribute itself
76  if (BasePath != null)
77  {
78  FileName = fileName.Replace(BasePath, string.Empty, StringComparison.InvariantCultureIgnoreCase);
79  }
80  else
81  {
82  FileName = fileName;
83  }
84  }
85  }
86 }