获取 XML 文档注释 [英] Get XML Documentation Comments

查看:47
本文介绍了获取 XML 文档注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/// <summary>  
///  This can have any description of the class  
/// </summary>  
public class MyClass {} 

在上面的代码示例中,有什么方法可以获得 summary 值.

In the above code sample, is there any way I could get the summary Value.

我的目标是在日志中记录对象的操作,在这种情况下,我知道哪个类在什么时候做了什么.

My objective in such this, is to record objects' actions in a log and in such context, I know what class did what and when.

我可以从 this.GetType().Name 获取类的名称,但在某些情况下,我需要有任何与之相关的描述.

I can get the Name of the class from this.GetType().Name but in some situation I will need to have any description associate with it.

如果有除 XML 文档之外的任何其他方法来描述类,那么我想听听.

If there is any alternative way of having a description of the class other than the XML documentation, then I would like to hear about it.

谢谢,

推荐答案

如果不想使用 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))

这篇关于获取 XML 文档注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆