PC Review


Reply
Thread Tools Rate Thread

CodeDom -- CodeMemberMethod

 
 
Mark
Guest
Posts: n/a
 
      3rd Jan 2005
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


 
Reply With Quote
 
 
 
 
Derek Harmon
Guest
Posts: n/a
 
      3rd Jan 2005
"Mark" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
> 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


 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      3rd Jan 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Mark" <(E-Mail Removed)> wrote in message

news:%(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
Derek Harmon
Guest
Posts: n/a
 
      3rd Jan 2005
"Mark" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
> 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


 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      3rd Jan 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Mark" <(E-Mail Removed)> wrote in message

news:%(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
Derek Harmon
Guest
Posts: n/a
 
      4th Jan 2005
"Mark" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non Virtual method using CodeMemberMethod -- CodeDOM Abhi Microsoft C# .NET 1 3rd Apr 2008 03:11 AM
Non Virtual method using CodeMemberMethod -- CodeDOM Abhi Microsoft Dot NET 0 1st Apr 2008 06:58 PM
CodeDom example Marco Trapanese Microsoft VB .NET 1 20th Mar 2007 12:21 PM
CodeDom Avi Perez Microsoft VB .NET 0 28th Mar 2005 09:55 PM
CodeDom: more than one Attribute for a CodeMemberMethod Kai Huener Microsoft C# .NET 2 26th Nov 2004 04:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:32 AM.