Get XML Documentation Comments

前端 未结 2 599
萌比男神i
萌比男神i 2021-01-27 04:55
///   
///  This can have any description of the class  
///   
public class MyClass {} 

In the above code sample, is th

相关标签:
2条回答
  • 2021-01-27 05:44

    If you don't want to use XML comments, you can store the description as metadata using the [Description] attribute, i.e:

    [Description("This can have any description of the class")]
    public class MyClass {} 
    

    You can then access the value of this attribute at run-time:

    public static string GetDescription(Type t) {
        return TypeDescriptor.GetAttributes(t)
            .OfType<DescriptionAttribute>()
            .Select(x => x.Description)
            .FirstOrDefault();
    }
    

    e.g. GetDescription(typeof(MyClass))

    0 讨论(0)
  • 2021-01-27 05:55

    You can't get the XML documentation from reflection, since it is not generated into the assembly. It is generated to a separate XML file, which you can open and read from your application. (Distribution might be an issue, since the XML file is generated after the assembly was compiled, so you can't include it as resource in the assembly itself.)

    An alternative solution might be to create a special attribute, which you can fill and read. Something like:

    [Summary("This can have any description of the class.")]
    public class MyClass {}
    

    The problem with that solution is that it doesn't generate the documentation XML, so you have no IntelliSense, etc.

    0 讨论(0)
提交回复
热议问题