CodeDom question for .net 2.0

A

Ashish

I am using the CodeDom namespace for generating classes, and would
really like to be able to use the CodeRegionDirective class to generate
regions around code blocks.

The only problem is that is looks like i can generate regions around a
single method or a single statement (using StartDirective) property, is
there any way to out a code region around a collection of statements, or
methods ?

any help would be appreciated..

TIA
 
N

Nicholas Paldino [.NET/C# MVP]

Ashish,

From what I can tell, you need to place two CodeRegionDirectives in your
code, one for the start, and one for the end. So, you find the method where
you want to place the region start, and add the CodeRegionDirective to the
StartDirectives collection.

You then go to the other element of code where you want to place the
region end, and then add the CodeRegionDirective to the EndDirectives
collection.

Hope this helps.
 
A

Ashish

Nicholas,

Thanks for the answer,
here is the code fargment ...

CodeTypeDeclaration ctd = new CodeTypeDeclaration();

ctd.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start,
"members"));
ctd.Members.AddRange(this.GetMemberFields());
ctd.StartDirectives.Add(new
CodeRegionDirective(CodeRegionMode.End,string.Empty));


but code generated by GetMemberFields is not places in the output :(

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Ashish,

Add the last CodeRegionDirective to the EndDirectives.
 
A

Ashish

Nicholas

thanks for pointing that out

i did that, but it adds the regions at the start of the whole class code
instead of just the members :(

for example the generated code looks like

#region members
public clas blah
{


// this is where i want it to start
private int mUId;
.......




}

#endregion



, any other clues ?
 
N

Nicholas Paldino [.NET/C# MVP]

Ashish,

The CodeMemberField class has StartDirectives and EndDirectives
properties which you can attach code directives to. Why not just attach
them to the one that corresponds to the mUId field?
 
A

Ashish

Nicholas,

well that would surround each member declaration with a region, and i
want to all the member declarations under one region, or there would be
too many regions to deal with .... , seems like this should be do able..



thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Ashish,

Right, so on the first member in the region, add the directive to the
StartDirectives. In the last member in the region, add the directive to the
EndDirectives.
 

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