views:

38

answers:

2

First here is the scenario.
I'm targeting 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() {
    }
}

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. 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?

Thanks a lot for your time.

Juan Zamudio

+2  A: 

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.

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.

I was afraid this was the answer, I was hoping that it was possible because StyleCop handle this situation correctly.
Juan Zamudio
StyleCop works against the original source code, so it knows whether the constructor was implicit or explicit. Most other static code analysis tools, and most documentation programs, work against the compiled assembly, where it is impossible to tell the difference.
+1  A: 

The following code will return information on any parameterless constructors in your type:

var info = typeof(MyClass).GetConstructor(new Type[] {});

I do not know of a way of differentiating between a default constructor and an explicitly specified parameterless constructor.

A possible workaround for your CommentChecker issue would be to explicitly create the parameterless constructor where one is required and comment it appropriately.

Ben Aston