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

查看:307
本文介绍了在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,有一个选项,以标准文档添加到所有默认的构造函数;所以真的没有必要记录它们。如果你有一个明确的默认构造函数在code,你可以把三个斜​​杠(///)构造上面 - 没有别的 - 禁用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天全站免登陆