Thursday, October 22, 2009

T4 Templates Natively Support Parameters!

Yippeee!

Starting in Beta 2, precompiled T4 templates support the parameter directive:

<#@ template language="VB" inherits="TemplateCommon.DotNetBase" #>

<#@ import  namespace="Universal.TemplateCommon" #>

<#@ import  namespace="AppVenture.Common" #>

<#@ parameter name="Fred" type="System.String" #>

 

Parameters aren’t supported yet in Intellisense (the add-in needs to catch up) and they aren’t highlighted with my set of Add-Ins. But the code above produces:

       Private _FredField As String

 

        '''<summary>

        '''Access the Fred parameter of the template.

        '''</summary>

        Private ReadOnly Property Fred() As String

            Get

                Return Me._FredField

            End Get

        End Property

 

And the Initializer attempts to assign the value to the parameter :

       Public Overrides Sub Initialize()

            MyBase.Initialize()

            If (Me.Errors.HasErrors = False) Then

                Dim FredValueAcquired As Boolean = False

                If Me.Session.ContainsKey("Fred") Then

                    If (GetType(String).IsAssignableFrom(Me.Session("Fred").GetType) = False) Then

                        Me.Error("The type 'System.String' of the parameter 'Fred' did not match the type of the da" & _

                                "ta passed to the template.")

                    Else

                        Me._FredField = CType(Me.Session("Fred"), String)

                        FredValueAcquired = True

                    End If

                End If

                If (FredValueAcquired = False) Then

                    Dim data As Object = Global.System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("Fred")

                    If (Not (data) Is Nothing) Then

                        If (GetType(String).IsAssignableFrom(data.GetType) = False) Then

                            Me.Error("The type 'System.String' of the parameter 'Fred' did not match the type of the da" & _

                                    "ta passed to the template.")

                        Else

                            Me._FredField = CType(data, String)

                        End If

                    End If

                End If

 

 

            End If

        End Sub

 

Thanks to Gareth Jones for listening to the occasionally loud input and making this happen!

 |  Kathleen Dollard  |  #    |  Comments [2]  |