|
-
Aug 17th, 2015, 02:52 PM
#1
[RESOLVED] [CodeDom] Putting code in a region directive?
According to the MSDN documentation, you create a #Region with the CodeRegionDirective class... but it is not obvious to me how to put code inside that region?
I want to generate:-
Code:
#Region "Private members"
Private Readonly m_Id As Integer
#End Region
Any ideas?
-
Aug 17th, 2015, 03:24 PM
#2
Re: [CodeDom] Putting code in a region directive?
That's exactly how you do it... in fact, when ever I used to build a class first thing I did was stub out my regions:
Code:
#Region "Private members"
#end Region
#Region "Properties"
#end Region
#Region "Private Functions"
#end Region
#Region "Private Methods"
#end Region
#Region "Public Functions"
#end Region
#Region "Public Methods"
#end Region
#Region "Event Handlers"
#end Region
I don't do that anymore because it's a maintenance nightmare... but you just simply create a region and stuff code into it. It is strictly an IDE organizational tool... nothing more.
-tg
-
Aug 17th, 2015, 04:59 PM
#3
Re: [CodeDom] Putting code in a region directive?
Ah - but how do I make this happen in CodeDOM? I can put a region around a whole class by adding these to the .StartDirectives and .EndDirectives of the CodeTypeDeclaration - but how to do this for just a collection of CodeStatement?
-
Aug 17th, 2015, 05:28 PM
#4
Re: [CodeDom] Putting code in a region directive?
I'm fairly sure the easiest way is to just include them in the code you pass to the compiler...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 17th, 2015, 05:44 PM
#5
Re: [CodeDom] Putting code in a region directive?
You only run the code with codedom. You don't actually see it in the IDE so what is the point of the #Region statements with codedom?
Burn the land and boil the sea
You can't take the sky from me
~T
-
Aug 17th, 2015, 09:38 PM
#6
Re: [CodeDom] Putting code in a region directive?
My bad, I didn't see that this was the through the CodeDom ... although I can't imagine it's all that different. As I mentioned, regions are a trick for the IDE, so the compiler is going to ignore them. If you're generating code that no one's really going to see, then I don't think there's much point in including them.
-tg
-
Aug 18th, 2015, 03:05 AM
#7
Re: [CodeDom] Putting code in a region directive?
The tool I am writing is going to create code that people are going to see - it will write the partial classes from the model (as XML) that makes the framework for the system which the developers then augment with their own code. (Like entity framework does etc.)
It appears I have to attaché the "start region" to the first CodeStatement in the collection and the "end region" to the last statement in the collection. I might just write a utility function for that :-)
-
Aug 24th, 2015, 03:58 AM
#8
Re: [CodeDom] Putting code in a region directive?
OK - simply adding the region directives to the first and last in the collection of code lines seems to work:-
VB Code:
Public Shared Sub WrapInRegion(ByVal RegionText As String, ByVal codeLines As ICollection(Of CodeTypeMember)) If (codeLines.Count > 0) Then codeLines(0).StartDirectives.Add(New CodeRegionDirective(CodeRegionMode.Start, RegionText)) codeLines(codeLines.Count - 1).EndDirectives.Add(New CodeRegionDirective(CodeRegionMode.End, String.Empty)) End If End Sub
This results in
VB Code:
'------------------------------------------------------------------------------ ' <auto-generated> ' This code was generated by a tool. ' Runtime Version:4.0.30319.42000 ' ' Changes to this file may cause incorrect behavior and will be lost if ' the code is regenerated. ' </auto-generated> '------------------------------------------------------------------------------ Option Strict Off Option Explicit On Namespace test Partial Public Class Duncan_s_Interface #Region "Private members" Private _Email As String Private _Height_cm As Integer #End Region End Class End Namespace
Last edited by Merrion; Aug 24th, 2015 at 04:04 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|