且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取 XML 文档注释

更新时间:2022-02-01 05:03:25

如果不想使用 XML 注释,可以使用 [Description] 属性将描述存储为元数据,即:

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();
}

例如GetDescription(typeof(MyClass))