C#文档注释 [英] C# documentation comments

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

问题描述

关于这篇文章,我的程序集设置为csc.exe / doc,所以我有一个包含文档评论的程序集的xml文件。

In relation to this post I have my assembly set up to csc.exe /doc so I have an xml file for the assembly that includes the documentation comments .

所以我有


  • MyAssembly.dll

  • MyAssembly.xml

现在,在xml中,它有

Now, looking at the xml, it has

<member name="M:MyAssembly.IFoo.SetTimer(System.Int32,System.Double)">

其中IFoo是程序集中定义的接口, SetTimer(int,double )是该接口中的一种方法。

where IFoo is an interface defined in the assembly and SetTimer(int, double) is a method in that interface.

我可以在xml中解析此字符串,但在我去之前,它有任何库,我可以用来从MethodInfo引用从这样的xml获取文档注释。在其中一个答案中提到了 sandcastle ,但它似乎都是关于MSDN风格的文档生成器我找不到任何让我做我想要做的例子。

I can go about parsing this string in xml but before I go about that route, it there any library that I could use to go from a MethodInfo reference to getting the documentation comments from such xml. There was a mention of sandcastle in one of the answers but it seems like it's all about MSDN-style documentation generator and I couldn't find any examples that would let me do what I'm trying to do.

谢谢

推荐答案

注释不会编译到程序集中,这意味着您无法以这种方式访问​​数据。尝试使用属性来定义额外的元数据:

Comments are not compiled into the assembly, which means you can't access data this way. Try using attributes to define additional metadata:

public class CommentAttribute : Attribute
{
  public CommentAttribute(string comment)
  {
    this.Comment = comment;
  }

  public string Comment { get; internal set; }
}

public static string GetComments(this ICustomAttributeProvider provider)
{
  var commentAttribute = provider.GetCustomAttributes(typeof(CommentAttribute), true)
    .Cast<CommentAttribute>()
    .FirstOrDefault();

  return (commentAttribute == null ? null : commentAttribute.Comment);
}

这样你可以传递一个 MethodInfo的实例,只需调用 GetComments()

That way you can pass in an instance of MethodInfo and simply call GetComments() on it.

在你的类型上,添加:

[Comment("This is metadata compiled in the assembly for this type")]
public class Foo { }

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

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