Part V: Other General Items Of Interest
Each representation of a member has a property named Attributes. Do not be fooled, this attribute is not used to specify attributes (metadata) that have been placed on a member.
Attributes are of the enum type MemberAttributes which is a flags enum:
|FamilyOrAssembly||0100||0000||0000 ||0000||protected internal||Protected Internal|
* Created only if the member also uses MemberAttributes.Override.
Important: Zero a mask before using any of the flags. Take the following code for example:
// Use the masks to set the access and scope flag spaces to
~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask;
// Bitwise set the Assembly and Static flags.
MemberAttributes.Assembly | MemberAttributes.Static;
This code would cause the member to be declared as internal and static (shared).
Should we fail to change the Attributes property, the member would be declared simply as a private member.
Note: In order to specify that a member is virtual, simply zero its scope mask:
member.Attributes &= ~MemberAttributes.ScopeMask;
The ugly part about the attributes we discussed is the use of the System.Reflection.TypeAttributes
enum with CodeTypeDeclaration
To specify that the type is either abstract (MustInherit) or sealed (NotInheritable), you should set CodeTypeDeclaration's TypeAttributes
// My class is abstract!
myDecl.TypeAttributes |= TypeAttributes.Abstract;
In order to place an attribute on a member (i.e. a class deriving from System.Attribute
), one should use the CustomAttributes property
If you have knowledge of the specific language in which the code is about to be generated, you can include Code Snippets, which basically give the ability to insert free text into the generated file.
This should be used only in harsh conditions, when all else fails, because it rather destroys the whole meaning of an independent code document object model. Examples: CodeSnippetTypeMember
Really. Don't use it.