[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?
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
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?
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...
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?
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
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 :-)
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