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]  | 
Thursday, November 26, 2009 8:43:33 AM (Mountain Standard Time, UTC-07:00)
Hello.
I m trying to use parameters.
All the generation is ok. In my generated code; and in the debugger; I have my field.

Do you know how set the paramter in VS2010 ?
Actually: I m calling the transformation like that:

string output = templateservice.ProcessTemplate("",specificTemplate, null, null);
I have tried :
CallContext.SetData("Message", M);
but it does not do anything.
After debugging i tried that; but it is not possible due to the protection level of textemplatinservice:
" ((Microsoft.VisualStudio.TextTemplating.Interfaces.ITextTemplatingSession)
((TextTemplatingService)templateservice).Session).Add(new KeyValuePair<string, object>("Message", M));"

Have you an idea ?


JAUME
Saturday, March 13, 2010 2:21:24 PM (Mountain Standard Time, UTC-07:00)
Hi Kathleen,

does this parameter stuff really work? So how do I specify a value for such a parameter when I have my *.tt file in VS2010? I'd love to be able to use it to create e.g. templates for various types of classes, but that would require me to be able to specify the namespace and class name (at least - possibly more) before I run the template, somehow..... works like a charm in things like CodeSmith, but doesn't seem to work with T4 templates - and not in VS2010 (Release Candidate) either, as far as I can tell. Thoughts??

Marc
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview