说我有
interface IFoo
{
/// <summary>
/// Comments about Bar method goes here.
/// </summary>
void Bar();
}
我正在使用反射在运行时显示方法
MethodInfo[] mis = typeof(IFoo).GetMethods();
但我想知道我是否可以将评论包含在 <summary> </summary>
中对于方法。我意识到注释只是忽略了我的编译器,但是有什么办法可以检索注释吗?现在我有一个单独的文件,其中包含方法和注释,但我讨厌冗余,想知道是否有任何方法可以做到这一点。
谢谢,
最佳答案
C# 编译器 csc.exe 有一个 /doc输出带有三重斜杠注释的外部 XML 文件的选项。此 XML 文件由文档生成器使用(例如 Sandcastle 做这种事情)。
导出 XML 注释的相同选项可从 Visual Studio 获得。要在 Visual Studio 开发环境中设置此编译器选项:
- 打开项目的属性页面。有关详细信息,请参阅如何:设置项目属性(C#、J#)。
- 单击构建属性页。
- 修改 XML 文档文件属性。
您可以使用 .NET 框架中的 XML 解析器加载此 XML 文件,访问其中的类型,并从它们周围获取相关注释。
您是对的,C# 编译器不会将注释编译到元数据中。但是,Microsoft 为导出功能创建了三斜杠注释,因此您可以处理它们。
Instructions for processing the XML file are here on MSDN .
例如,我启用了 XML 输出文件选项并记录了以下方法:
/// <summary>
/// This method parses the given name for
/// capitalization.
/// </summary>
public void ParseStringCase(string name)
{
// behaviour of method...
}
它在 bin/文件夹中的文件中生成以下 XML....
<?xml version="1.0"?>
<doc>
<assembly>
<name>WindowsFormsApplication3</name>
</assembly>
<members>
<member name="M:WindowsFormsApplication3.Form1.ParseStringCase(System.String)">
<summary>
This method parses the given name for
capitalization.
</summary>
</member>
</members>
</doc>
关于c#获取接口(interface)方法注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5859097/