I've been all over the place trying to find a way to generate a file from a T4 template in my project.
My goal is to :
- Create a templat开发者_高级运维e
- Include it somewhere (I don't know where)
- Do "New file from template"
- Generate a new .cs file in my project from the template result
If you can guide me or share a tutorial doing that I'd be quite happy!
Here you'll find Microsoft's T4 documentation. You'll need to be aware, however, that Microsoft's support for T4 is not that complete overall and AFAIK it relies on 3rd party T4 editors for creating the templates. Such as Tangible.
There's a nice package on NuGet from Steve Sanderson called T4Scaffolding that does this kind of thing.
http://nuget.org/packages/T4Scaffolding/
If that doesn't work for you, you can roll your own.
To do this, you'd use T4's service API from a Visual Studio Item Template.
You can do this by building a small COM component implementing the IWizard interface.
Here's the interface docs: http://msdn.microsoft.com/en-us/library/ms233750(v=VS.100).aspx
Here's an example of creating an IWizard (not a T4 one): http://blogs.conatural.com/2009/01/how-to-write-a-vs-2008-item-template-and-wizard-for-conatural-data-commands/comment-page-1/
Here's the T4 service interface that you can call from your wizard to run T4 on an arbitrary file: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.texttemplating.vshost.itexttemplating.aspx
Use the following code on any handy service provider to get hold of this:
GetService(typeof(STextTemplating)) as ITextTemplating
精彩评论