Code Generation Misbehaving

The System.CodeDom.Compiler.CodeGenerator class has a private method called GenerateType which takes a type and generates its comments, members, etc.
The thing is, that you can’t really determine the order in which the members will be generated.
This is quite annoying all by itself (you can’t generate pre-processor directives wherever you want them), but acceptable, since there has to be some logic behind the intention of the programmer who wrote this method.

Before I used the Microsoft.CSharp.CSharpCodeGenerator class, which derives from it, I created a CodeDOM graph and wanted to use it.
The collection which holds all the members to be generated is of type System.CodeDom.CodeTypeMemberCollection which has index-specific methods such as Insert, IndexOf and an index-bound CopyTo.

My question is this:
Why create methods that would mislead programmers that don’t want to dive into the code? If the index doesn’t matter, why can I work with it?
And if your answer is “Because you might have other code generators that would want to generate members according to the index”, why isn’t there a single piece of documentation about the fact that indexes don’t matter in the current generators?

Update: I just noticed that I talked about this, but haven’t given the order in which members are written, so here it is:

  1. Type Comments
  2. Start of Type Declaration
  3. Fields
  4. Code Snippets
  5. Type Initializer (Static Constructor)
  6. Instance Constructors
  7. Properties
  8. Events
  9. Methods
  10. Nested Types
  11. End of Type Declaration


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s