index > Visual Studio Guidance Automation Toolkit > Passing custom objects to t4 templates

Passing custom objects to t4 templates

This question has been asked before but I have a new twist for you. Looking to pass my own custom objects to a t4 template by calling TextTemplateAction from within my own Action class. The problem is that TextTemplateAction.Configure() takes a StringDictionary for the template args. Are there any other ways to pass this along? I've basically done the following

1. Set up a RecipeArgument based on my custom objects type.

2. Specified that my custom action (the one which calls TextTemplateAction) should receive this RecipeArgument.

3. Configured the template to import namespaces and set assembly references to the custom object and attempted to use an instance of it within the template.

This results in

=============================================================================

Microsoft.Practices.RecipeFramework.ActionExecutionException: An exception occurred during the binding of reference or execution of recipe GenerateEntity. Error was: Action ProcessRegionValueObject failed to execute:
Exception of type 'Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TemplateException' was thrown..
You can remove the reference to this recipe through the Guidance Package Manager. ---> warning : An Exception was thrown while processing a directive named 'Property'. The following Exception was thrown:
System.ArgumentNullException:

Template uses property name which has not been received for execution.
Parameter name: CodeGenOptions

at Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.PropertiesDirectiveProcessor.ProcessDirective(String directiveName, IDictionary`2 arguments)
at

===============================================================================

Should I just try and pull the instance from the Dictionary service within the Template itself?

Any ideas would be greatly appreciate (we're stuck here)

Thanks,

Travis




Travis
tplummer
You should not try to execute the text template action from another action. Try to split your current action so that it leaves output that is used by the built-in action to do some process inside the template.

The template action Configure method is NOT for passing argument values, but for passing a string dictionary of the values that would have been used in config for the action, such as the Template (a path relative to the \Templates folder in your package). In order to pass argument values, you need to use TypeDescriptor.GetProperties(templateAction)["MyArgument"] = myValue. That is the mechanism that allows passing unbounded number of input properties that are not actually defined in the action itself, but in the inner template.

The inner workings of this action is more complicated than it seems. Let me know if either of these approaches work.




Daniel Cazzulino [Solutions Architect MVP]
kzu

Hey kzu,

Thanks for your response. We've already looked at the TypeDescriptor approach. Unfortunatly, we get a NotSupportedException back. The docs for TypeDescriptor seem to indicate that this is due to the the passed object (the action in our case) being a cross-process remoted object. (Action is-a Component which is-a MarshalByRefObject :(

TypeDescriptor Docs -> http://msdn2.microsoft.com/en-us/ybh0y4fd.aspx

Another thread describing this situation.. ->http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=56690&SiteID=1

This is one approach that we've looked at. Another angle is attempting to pull values from the dictionary within the template itself rather than using the Property Processor \ Template args.. Something like so...

==============================================================================

<#@ Template language="C#" #>

<#@ Assembly Name="System.dll" #>

<#@ Import Namespace="System.ComponentModel" #>

<#@ Import Namespace="System" #>

// SET ASSEMBLY REFS AND IMPORT THE CORRECT NAMESPACES FOR OUR CUSTOM OBJECT HERE..

<#

System.ComponentModel.Design.IDictionaryService svc = (System.ComponentModel.Design.IDictionaryService)GetService(typeof(System.ComponentModel.Design.IDictionaryService));

// PULL CUTOM OBJECT INTO A TEMPLATE\GLOBAL SCOPED INSTNACE HERE...

#>

==============================================================================

Here, we would just build up the dictionary values we need within the wizards, and pull them out within the templates (I think this is the cleanest design from a customers perspective). We've seen this approach work for people using T3 (We're on T4), but we get TemplateExceptions indicating that GetService is not valid in this context (as if the interpreter doesn't derive from or have access to Component.GetService()??)

Also, the HOL package does get a DataTable for use by the template. We're doing something similiar to the HOL yet with our own objects instead of a DataTable. Our next steps are to not nest actions as you've suggested. This is ultimately bad for us as the recipe configs get real ugly (yet software does get ugly sometimes).

Travis




Travis
tplummer
reply 3

You can use google to search for other answers

 

More Articles

• Error 1001: Item has already been added. Key in dictionary
• How can you use T3 templates in VS templates???
• Slow Wizard Input using MonitorArgument
• Minor bug in VszWizard.FindAddItem
• Making compiling faster!!!
• Get Full Path for Project Item...
• classname value
• Reuse Projects and Recipes
• Can't run the Guidance Package
• Uninstalling Missing Packages
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Package version
• Using FileNameEditor as a Value Editor
• GAT Wrapper
• Type of Document objects retrieved from
• How to add template menu item to custom
• Error "System.ArgumentNullException
• Problem Loading Package
• Future Roadmap for GAT
• Problem Loading Package
• Can't run the Guidance Package
• Project templates do not appear in VS 20
• Custom Wizards and custom arguments
• Error Installing to RTM Version of VS2005
• GAT fails to install, claims GAX is miss
• How do you include a folder that has bee

Hot Articles

• Error installing GAX
• Trying to add a new class to the Referen
• Smart Client Software Factory (Recipe Fr
• ValueProvider for the first selected Ite
• Factoring Factories :)
• new to GAT
• Can't download GAT, GAX?
• Custom UnfoldTemplate
• Visual Studio hangs when I install the T
• Referencing a generic class with a t4 te
• How can you skip the UI when you run a r
• Uninstalling Missing Packages
• Parameterising ProjectType in VS2005 tem
• Has anyone written a MSI bootstrapper fo
• Making compiling faster!!!

Recommend Articles

• GAT on Visual Studio Release Candidate
• Guidance Package Build
• Error install GAX
• Fail to register Guidance Package from S
• GAT fails to install, claims GAX is miss
• Loosing Item Template Information
• GAT Wrapper
• classname value
• What is the benefit of using GAT versus
• When is the next release for GAT???
• Queued Recipe Execution
• Cannot install GAT or any other guidance
• Help me decide: how to generate my Data
• Guidance Package Visual Studio 2005 Proj
• Installation error