是否可以在运行时获取类摘要?
C# 是否可以在运行时获取类摘要? 我想通过反射获取类摘要,然后将其写入控制台。 我所说的类摘要是指类定义之前的摘要注释,如下所示:
/// <summary>
/// some description
/// </summary>
class SomeClass
{
}
我不知道这些注释在编译代码后是否可用,但如果可用,也许有一种方法可以在代码中获取它们。
预先感谢您的帮助。
Is it possible to obtain class summary at runtime in C#?
I would like to obtain class summary through reflection and then write it to console.
By class summary I mean summary comments before class definition, something like this:
/// <summary>
/// some description
/// </summary>
class SomeClass
{
}
I don't know if these comments are available after compiling the code, but if they are maybe there is a way to obtain them in code.
Thanks in advance for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不久前我曾经搞乱过这个问题,并使用了这个人的解决方案。 效果很好:
http://jimblackler.net/blog/?p=49
I once messed with this a while back, and used this guys solution. Worked pretty good:
http://jimblackler.net/blog/?p=49
我在 CodePlex 上维护 Jolt.NET 项目,并实现了执行此任务的功能。 请参阅 Jolt 库了解更多信息。
实质上,该库允许您使用
System.Reflection
中的元数据类型(即MethodInfo
、PropertyInfo)以编程方式查找和查询程序集的 XML 文档注释文件。
等...)。I maintain the Jolt.NET project on CodePlex and have implemented a feature that performs this very task. Please refer to the Jolt library for more information.
In essence, the library allows you to programatically locate and query an XML doc comments file for an assembly using the metadata types in
System.Reflection
(i.e.MethodInfo
,PropertyInfo
, etc...).不,它们无法通过反射获得。 请参阅 msdn:
Nope, they're not available through reflection. See msdn:
您无法在运行时访问它们,因为编译器将它们视为注释。
但是,如果您想使用属性来指定信息并在运行时通过反射访问它,您可以这样做。
有关属性创建和 创建自定义属性(C# 编程指南),请参阅a href="http://msdn.microsoft.com/en-us/library/z919e8tw.aspx" rel="nofollow noreferrer">通过反射访问属性(C# 编程指南) 用于运行时访问。
来自 MSDN 的示例:
Author.cs:
SampleClass.cs:
You cannot access those at runtime because those are considered to be comments by the compiler.
However, if you wanted to use an Attribute to specify information and access it during runtime via reflection you could do that.
See Creating Custom Attributes (C# Programming Guide) for attribute creation and Accessing Attributes With Reflection (C# Programming Guide) for runtime access.
Example from MSDN:
Author.cs:
SampleClass.cs:
如果您发出 XML 文档文件,则可以。 该过程将涉及使用反射来获取该类型的所有公共成员,然后使用 XPath 从生成的 XML 文档中读取文档。
更新:要将 XML 文档包含在 dll/exe 中,只需将其添加为嵌入式资源,并在文档发生更改时编译两次。
You can, if you emit an XML documentation file. The process would involve using reflection to get all the public members of the type, then using XPath, read the documentation from the generated XML document.
UPDATE: to include the XML doc in your dll/exe, just add it as an embedded resource, and compile twice if documentation changes.
不,这些注释不包含在您编译的程序集中。
Visual Studio 可以在输出文件夹 (\bin\your_project.xml) 中创建一个包含这些注释的 .xml 文件。 如果您的应用程序随该 xml 文件一起分发,那么您将能够以编程方式访问它。
No, those comments are not included in your compiled assembly.
Visual Studio can create a .xml file in your output folder (\bin\your_project.xml) which contains those comments. If your application were distributed with that xml file then you would be able to access it programatically.