CodeDom -- CodeMemberMethod

M

Mark

I'm using the CodeDom namespace to generate code. How do you specify that
the method of a class should be static? I'm assuming I use the
CodeMemberMethod class to define the method, but I'm not seeing the property
of the class that would allow me to specify that the method should be
static.

Thanks in advance.

Mark
 
D

Derek Harmon

Mark said:
I'm using the CodeDom namespace to generate code. How do you specify that
the method of a class should be static? I'm assuming I use the
CodeMemberMethod class to define the method, but I'm not seeing the property
of the class that would allow me to specify that the method should be
static.

Mark,

You've got the right class, CodeMemberMethod. What you need to do is mark the
method as static by adding a flag [MemberAttributes.Static] to the bitmask of the
Attributes property (which CodeMemberMethod inherits from CodeTypeMember),
like this,

codeMethod.Attributes |= MemberAttributes.Static;


Derek Harmon
 
M

Mark

Useful. Thanks. However, I have two follow-up questions:

1. What is the purpose of the |= operator here?
2. I'd like to make it static AND public. It appears to only allow you to
do one ... if you do both in a row, the second prevails. The default
behavior is private and an instance method. Is there a workaround?

CodeMemberMethod method = new CodeMemberMethod();
method.Name = "MyMethod";
method.Attributes = MemberAttributes.Static;
method.Attributes = MemberAttributes.Public; //This one overwrites the
other!!

Thanks again!

Mark



Derek Harmon said:
I'm using the CodeDom namespace to generate code. How do you specify that
the method of a class should be static? I'm assuming I use the
CodeMemberMethod class to define the method, but I'm not seeing the property
of the class that would allow me to specify that the method should be
static.

Mark,

You've got the right class, CodeMemberMethod. What you need to do is mark the
method as static by adding a flag [MemberAttributes.Static] to the bitmask of the
Attributes property (which CodeMemberMethod inherits from CodeTypeMember),
like this,

codeMethod.Attributes |= MemberAttributes.Static;


Derek Harmon
 
D

Derek Harmon

Mark said:
1. What is the purpose of the |= operator here?

|= is a special C# assignment operator that combines assignment (=) with a bitwise-OR (|).
You may be more familiar with the more common +=,

sum += item.Value;

which is equivalent to,

sum = sum + item.Value;

Similarly,

codeMethod.Attributes |= MemberAttributes.Static;

is equivalent to,

codeMethod.Attributes = codeMethod.Attributes | MemberAttributes.Static;

which says the Attributes are the same as they are originally, but the Static
flag is turned on (if it isn't already on).
2. I'd like to make it static AND public. It appears to only allow you to
do one ... if you do both in a row, the second prevails. The default
behavior is private and an instance method. Is there a workaround?

Attributes is a [Flags] enumeration. If you're familiar with C programming, you
may have encountered enums that were used to create "bitmasks" such as,

enum MyFlags {
XMIT_CHAR = 1,
RECV_CHAR = 2,
CARRIER = 4
};

and in this case you could have an int variable that could contain none-, one-,
two- or all three- of these binary bit flags. You would turn ON a flag by using
a bitwise-OR operation, or you would turn OFF a flag by using a bitwise AND
operation. For example (this is in C pseudocode),

/* auxStatReg is 5, it has bits 0 and 2 turned ON. */
int auxStatReg = XMIT_CHAR | CARRIER;

/* auxStatReg is 4, bit 0 has been turned OFF. */
auxStatReg = auxStatReg & CARRIER;

/* auxStatReg is tested to see whether bit 2 is turned ON. */
if ( ( auxStatReg & CARRIER ) == CARRIER )
/* we have carrier. */
else
/* no carrier. */

These operations continue to be used in C#, in fact they've been improved and
enum declarations that have a [FlagsAttribute( )] applied to them automatically
support these sorts of operations with strong compile-time type checking (and
meaningful identifier names when debugging in the IDE).

In this case, Attributes is of type MemberAttributes which is a [Flags] enum.
That means you can turn on multiple flags, but working with them means you
need to be familiar with using the bitwise-OR and -AND operations.
CodeMemberMethod method = new CodeMemberMethod();
method.Name = "MyMethod";
method.Attributes = MemberAttributes.Static;
method.Attributes = MemberAttributes.Public; //This one overwrites the
other!!

You can use the bitwise-OR operator, |, to turn on both flags.

// Makes the Attributes Static and Public (overwrites previous Final, Private
// flag selections).
//
method.Atributes = MemberAttributes.Static | MemberAttributes.Public;

If you're familiar with Set Theory, think of OR as performing a Union, and
think of AND as performing an Intersection.


Derek Harmon
 
M

Mark

THANK YOU! I really appreciate the thorough response. This makes a lot of
sense - I've never worked with bitwise operators before, but I now certainly
see their value.

I'm hoping my final question is simple -- what indicates that
CodeMemberMethod.Attributes requires bitwise operations or that it allows
for more than one bit to be set? The word "attributes" below is plural, but
is there another clear indication? I'm looking at the class library and
don't see anything about the Attributes property allowing for multiple
values. I would assume that by NOT specifying this more clearly, it would
only allow one ... but perhaps I just need to watch for the plural part.

Thanks again. - Mark

**** FROM CLASS LIBRARY ***
CodeTypeMember.Attributes Property [C#]

Gets or sets the attributes of the member.

[C#]
public MemberAttributes Attributes {get; set;}

Property Value
One of the MemberAttributes values that indicates the attributes of the
member. The default value is Private | Final.

Remarks
This property is used to specify attributes of the type member. Some types
of attributes are Access (Public/Private) and Scope (Final/Override).

**** END CLASS LIBRARY ***





Derek Harmon said:
1. What is the purpose of the |= operator here?

|= is a special C# assignment operator that combines assignment (=) with a bitwise-OR (|).
You may be more familiar with the more common +=,

sum += item.Value;

which is equivalent to,

sum = sum + item.Value;

Similarly,

codeMethod.Attributes |= MemberAttributes.Static;

is equivalent to,

codeMethod.Attributes = codeMethod.Attributes | MemberAttributes.Static;

which says the Attributes are the same as they are originally, but the Static
flag is turned on (if it isn't already on).
2. I'd like to make it static AND public. It appears to only allow you to
do one ... if you do both in a row, the second prevails. The default
behavior is private and an instance method. Is there a workaround?

Attributes is a [Flags] enumeration. If you're familiar with C programming, you
may have encountered enums that were used to create "bitmasks" such as,

enum MyFlags {
XMIT_CHAR = 1,
RECV_CHAR = 2,
CARRIER = 4
};

and in this case you could have an int variable that could contain none-, one-,
two- or all three- of these binary bit flags. You would turn ON a flag by using
a bitwise-OR operation, or you would turn OFF a flag by using a bitwise AND
operation. For example (this is in C pseudocode),

/* auxStatReg is 5, it has bits 0 and 2 turned ON. */
int auxStatReg = XMIT_CHAR | CARRIER;

/* auxStatReg is 4, bit 0 has been turned OFF. */
auxStatReg = auxStatReg & CARRIER;

/* auxStatReg is tested to see whether bit 2 is turned ON. */
if ( ( auxStatReg & CARRIER ) == CARRIER )
/* we have carrier. */
else
/* no carrier. */

These operations continue to be used in C#, in fact they've been improved and
enum declarations that have a [FlagsAttribute( )] applied to them automatically
support these sorts of operations with strong compile-time type checking (and
meaningful identifier names when debugging in the IDE).

In this case, Attributes is of type MemberAttributes which is a [Flags] enum.
That means you can turn on multiple flags, but working with them means you
need to be familiar with using the bitwise-OR and -AND operations.
CodeMemberMethod method = new CodeMemberMethod();
method.Name = "MyMethod";
method.Attributes = MemberAttributes.Static;
method.Attributes = MemberAttributes.Public; //This one overwrites the
other!!

You can use the bitwise-OR operator, |, to turn on both flags.

// Makes the Attributes Static and Public (overwrites previous Final, Private
// flag selections).
//
method.Atributes = MemberAttributes.Static | MemberAttributes.Public;

If you're familiar with Set Theory, think of OR as performing a Union, and
think of AND as performing an Intersection.


Derek Harmon
 
D

Derek Harmon

Mark said:
what indicates that CodeMemberMethod.Attributes requires bitwise operations or that it allows
for more than one bit to be set?

Most such enumerations are marked by the [FlagsAttribute( )], which is a special
metadata attribute that gets compiled into the assembly alongside the Type's info
(see the help on System.FlagsAttribute for more information here).

Here it can be inferred because there are multiple MemberAttributes that can apply
simultaneously (for example, Family and Static for "internal static" members).


Derek Harmon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top