Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

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

Moaz said:

Hello Omaer van,

I have being looking at Code Dom for quite some time. I have being trying to emit a STATIC class but it's not working. As far as I understand the CodeTypeDeclaration.TypeAttributes  is the property that should be looked at Correct me if I am wrong.

# December 11, 2007 2:36 AM

Omer van Kloeten said:

Moaz - CodeDom has been pretty much neglected in frameworks 2.0 and up, so this just doesn't exist.

You could, on the other hand, simply create an abstract sealed class and emit only static members into it. 

# December 11, 2007 2:01 PM

Moaz said:

Thanks Omaer van

# December 12, 2007 7:15 AM

Alexandra said:

I tried to use (I need protected override attributes) :

InitializeBindingsMember = new CodeMemberMethod();

InitializeBindingsMember.Name = "InitializeBindings";

                   InitializeBindingsMember.Attributes &= ~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask;

                   InitializeBindingsMember.Attributes |= (MemberAttributes.Override| MemberAttributes.Family);

declaration.Members.Add(InitializeBindingsMember);

because I want to generate:

protected override void InitializeBindings()

{

}

but override attrib is not generated :-(((((

I have only:

protected void InitializeBindings()

{

}

Do you have any idea why?

I use Framework 3.5

Thanks in advance, Alexandra

# March 6, 2008 9:23 AM

4s|m3tr|ko0 said:

I suppose you've already solved your issue with different masks and attributes, but I found in msdn a way to do it mask and attribute by mask and attribute instead of resetting all masks first and all setting attributes then:

InitializeBindingsMember.Attributes = (InitializeBindingsMember.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Override;

InitializeBindingsMember.Attributes = (InitializeBindingsMember.Attributes & ~MemberAttributes.ScopeMask) | MemberAttributes.Family;

That should do the trick.

# June 15, 2008 8:31 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)