Get XML Documentation Comments

前端 未结 2 598
萌比男神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()
            .Select(x => x.Description)
            .FirstOrDefault();
    }
    

    e.g. GetDescription(typeof(MyClass))

提交回复
热议问题