Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Note: This blog has moved to omervk.wordpress.com.

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Part V: Other General Items Of Interest

Attributes

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:

Member Binary Representation C# VB.NET
AccessMask1111000000000000
Assembly0001000000000000internalInternal
FamilyAndAssembly001000000000 0000private*Internal*
Family0011000000000000protectedProtected
FamilyOrAssembly010000000000 0000protected internalProtected Internal
Private0101000000000000privatePrivate
Public0110000000000000publicPublic
VTableMask**0000000011110000
New0000000000010000newShadows
ScopeMask0000000000001111
Abstract0000000000000001abstractMustInherit
MustOverride
Final0000000000000010sealed*NotInheritable*
Static0000000000000011staticShared
Override0000000000000100overrideOverrides
Const0000000000000101constConst
Overloaded0000000100000000N/AOverloaded

* 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
// undefined.
myMember.Attributes &=
	~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask;
// Bitwise set the Assembly and Static flags.
myMember.Attributes |=
	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;

Type Attributes

The ugly part about the attributes we discussed is the use of the System.Reflection.TypeAttributes enum with CodeTypeDeclarations.
To specify that the type is either abstract (MustInherit) or sealed (NotInheritable), you should set CodeTypeDeclaration's TypeAttributes property.
e.g.:
// My class is abstract!
myDecl.TypeAttributes |= TypeAttributes.Abstract;

Custom Attributes

In order to place an attribute on a member (i.e. a class deriving from System.Attribute), one should use the CustomAttributes property.
e.g.:
myMember.CustomAttributes.Add(new CodeAttributeDeclaration(typeof(ObsoleteAttribute));

Snippet Classes

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, CodeSnippetCompileUnit and CodeSnippetExpression.
Really. Don't use it.

Comments

No Comments