1
How to reference an attribute on a method or property?
Question asked by Henry G - 3/8/2019 at 4:22 AM
Answered
Is it possible to reference an attribute on a language element, such as a property or method, in auto-generated help documentation?  If so, how?
 
For example:
        [CustomProperty]
        public bool IsAvailable
        {
            get;
        }
How can we reference "CustomProperty" in template text here?

1 Reply

Reply to Thread
0
Serge B. Replied
Employee Post Marked As Answer
Hi Henry,
 
Please see GhostDoc help for SubMain.GhostDoc.T4.Attribute class. You can retrieve the list of current member attribute names in the template using Context.CurrentCodeElement.Attributes
 
Keep in mind, currently, you only get the list of the attribute names and don't have access to the values (if any). The latter is on our list to add.
 
See the IsTestMethod at the end of the Method template for the code sample.
 
Thanks!
 
EDIT - I figured I'd post that snippet here so you don't have to go look for it.
private bool IsTestMethod()
{
  SubMain.GhostDoc.T4.Attribute[] attributes = Context.CurrentCodeElement.Attributes;
  if(attributes != null)
  {
    foreach(SubMain.GhostDoc.T4.Attribute attribute in attributes)
    {
      if(string.Compare(attribute.Name, "TestAttribute", false) == 0 || string.Compare(attribute.Name, "Test", false) == 0 ||
      string.Compare(attribute.Name, "TestCaseAttribute", false) == 0 || string.Compare(attribute.Name, "TestCase", false) == 0 ||
      string.Compare(attribute.Name, "TestMethodAttribute", false) == 0 || string.Compare(attribute.Name, "TestMethod", false) == 0 ||
      string.Compare(attribute.Name, "FactAttribute", false) == 0 || string.Compare(attribute.Name, "Fact", false) == 0 ||
      string.Compare(attribute.Name, "TheoryAttribute", false) == 0 || string.Compare(attribute.Name, "Theory", false) == 0 ||
      string.Compare(attribute.Name, "RowTestAttribute", false) == 0 || string.Compare(attribute.Name, "RowTest", false) == 0 ||
      string.Compare(attribute.Name, "RowAttribute", false) == 0 || string.Compare(attribute.Name, "Row", false) == 0)
      {
        return true;        
      }
    }
  }
  return false;
}
 
Serge Baranovsky
SubMain Software
(800) 936-2134

Reply to Thread