检测编译器生成的默认构造函数使用C# [英] Detect compiler generated default constructor using reflection in C#

查看:207
本文介绍了检测编译器生成的默认构造函数使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是.NET 3.5 SP1,我使用 CommentChecker 验证我的XML文档,一切正常,直到我得到一个类像这样:

I'm targeting .NET 3.5 SP1 and I'm using CommentChecker to validate my XML documentation, everything works OK until I get to a class like this:

/// <summary>
/// documentation
/// </summary>
public sealed class MyClass {
    /// <summary>
    /// documentation
    /// </summary>
    public void Method() {
    }
}

上面的例子,如我所知,编译器生成我的类的默认构造函数。问题是,CommentChecker生成警告告诉我构造函数缺少注释。

In the example above, as I understand, the compiler generates a default constructor for my class. The problem with this is that CommentChecker generates warnings telling me that the constructor is missing the comments.

我试图修改程序来检测这个特殊情况,忽略它,但我坚持,我已经尝试过 IsDefined(typeof(CompilerGeneratedAttribute ),true)但是不起作用。

I tried to modify the program to detect this special case and ignore it but I'm stuck, I already tried with IsDefined(typeof(CompilerGeneratedAttribute), true) but that did not work.

简而言之,如何使用反射检测默认构造函数?

So in short, how can I detect the default constructor using reflection?

推荐答案

没有办法通过元数据检测自动生成的默认构造函数。你可以通过创建一个有两个类的类库来测试它,一个有一个显式的默认构造函数,一个没有。然后在程序集上运行ildasm:两个构造函数的元数据是相同的。

There is no way to detect automatically generated default constructors through metadata. You can test this by creating a class library with two classes, one with an explicit default constructor, and one without. Then run ildasm on the assembly: the metadata of the two constructors is identical.

而不是试图检测生成的构造函数,我只是更改程序,任何默认构造函数。大多数文档生成程序,如NDoc和SandcastleGUI,都有一个选项,可以向所有默认构造函数添加标准文档;所以它真的没有必要记录他们。如果你的代码中有一个显式的默认构造函数,你可以在构造函数上面加上三个斜杠(///) - 没有别的东西 - 禁用Visual Studio关于缺少文档的警告。

Rather than try to detect generated constructors, I would simply change the program to allow missing documentation on any default constructor. Most documentation generation programs, like NDoc and SandcastleGUI, have an option to add standard documentation to all default constructors; so it's really not necessary to document them at all. If you have an explicit default constructor in your code, you can put three slashes (///) above the constructor - nothing else - to disable the Visual Studio warning about missing documentation.

这篇关于检测编译器生成的默认构造函数使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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