Pass Object to T4 Template

I have a u1class object User. The user has an attribute name. How to transfer u1the T4 template? I am trying to do something very simple:

User u1 = new User("John"); 


Script Template:

Hello <# u1.Name #>
+5
source share
5 answers

Do you want to use it in your template? Then you need to add a link to the assembly containing this type. You cannot simply transfer the existing link to the T4 engine if you do not accept it yourself in some very unorthodox manner (I have never seen anyone try to do this). And even if you went so far and did it, how would you run it? Where should this link come from?

T4 , / stub <#+ /*stuff goes here*/ #>.

+2

- . :

  • ( "inherits" )
  • ( "hostspecific" )

, .

interface ICallbackInterface
{
    void CallbackFxn();
}

[Serializable]
public class MyCustomHost : ITextTemplatingEngineHost, ITextTemplatingSessionHost, IStencilFileRecordManagement
{

    private ICallbackInterface callback = null;
    public MyCustomHost(ICallbackInterface cb)
    {
        callback = cb;
    }

    public void CallbackFxn()
    {
        callback.CallbackFxn();
    }
}

public abstract class MyTemplateBase : TextTransformation
{
    public virtual MyCustomHost CustomHost
    {
        get
        {
            dynamic metame = this;
            MyCustomHost rval = null;
            try
            {
                /// <summary>
                /// The "Host" property will be added to the generated class by the T4 environment whenever a 
                /// "hostspecific" template is processed. 
                /// </summary>
                rval = metame.Host as MyCustomHost;
            }
            catch (RuntimeBinderException e)
            {
                logger.ErrorException(
                    "Received the following exception while processing a stencil template", e);

            }
            return rval;
        }
    }
}

, , , , :

<# CustomHost.CallbackFxn(); #>

, T4 VS - Microsoft.VisualStudio.TextTemplating.10.0 Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.

T4, . , :

using Microsoft.VisualStudio.TextTemplating;

class UserPluginWorkflowComponent : ICallbackInterface
{
    public void CallbackFxn()
    {
        // invoked by user plugin
    }

    public void ExecuteUserPlugin()
    {
        MyCustomHost host = new MyCustomHost(this);
        host.TemplateFileValue = "UserPluginTemplateFilename";
        Engine engine = new Engine();
        string pluginResult = engine.ProcessTemplate(
            userPluginTemplateFileContents, 
            host);            
        if (!host.Errors.HasErrors)
        {
            // use pluginResult in some meaningful way
        }
    }
}
+3

T4 , , , , "" - . Template , , . , , , .

, t4, , .

+1

, - , , ( ) , < # + # > , < # @# > , .

: http://msdn.microsoft.com/en-us/library/gg586944.aspx

+1

Hey - you need to set values, etc. inside a T4 script, so initialization Usershould happen inside the tag <# #>, which will turn your example into something similar to

     <# User u1 = new User() { .Name = "John" } #>
     Hello <# u1.Name #> 

As mentioned above, you may also need to import namespaces. I used the instructions below at the beginning of a TT file to access System.Data:

     <#@ import namespace="System.Data" #>
     <#@ import namespace="System.Data.SqlClient" #>
     <#@ assembly name="System.Data" #>
0
source

All Articles