How to generate a simple class in T4?

I create T4 - I press Ran custom, and it generates some working class in the style:

#line hidden /// <summary> /// Create the template output /// </summary> public virtual string TransformText() 

How to make it like T4MVC, only ready-made classes.

For example: for T4 to create three classes with an int testField {get; set;} field int testField {get; set;} int testField {get; set;} .

UPD. http://t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html

  • Hands, all hands. What's in your .t4 file? - VladD
  • What hands? Suppose I need T4 to create three classes with an int field testField {get; set;}. How to be? - Ssss
  • @sss: Well, show me what you have. T4 can do everything, but it does not work at that level. It generates a text file, and the source code of the class or “War and Peace” will be in this file, it depends on you personally. I'll bind hellowworld to you. - VladD
  • "<# @ template language =" C # "#> <# @ assembly name =" System.Core "#> <# @ import namespace =" System.Linq "#> <# @ import namespace =" System.Text "# > <# @ import namespace = "System.Collections.Generic" #> "Just a clean template. Ie, T4 generates Calss, which methods can generate anything? How then are all sorts of EF model generators and T4MVS built? Their structure is a little different - in the actual .tr file of the autogeneration rule and everything and somehow onos once creates everything. - Ssss
  • @sss: Well, there still, probably, external .t4-files are connected, which, using model structures as a description, generate code. - VladD

1 answer 1

T4 can do everything, but it does not work at that level. It generates a text file, and the source code of the class or “War and Peace” will be in this file, it depends on you personally.

Class generation is done like this:

 <#@ template language="C#" #> <#@ output extension=".cs" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <# var classNames = new[] { "ClassA", "ClassB", "ClassC" }; foreach (var name in classNames) { #> public class <#= name #> { public int TestField { get; set; } } <# } #> 
  • Thank you, but after Ran Custom the files did not appear, only test.сs for test.tt has changed. Perhaps I’m missing something, but for examples of ready-made tools, I was expecting the creation of nested classes. - Ssss
  • @sss: Well, yes. And in test.cs , the code for the three classes should appear. Appeared? --- test.cs - this is where output takes place, and where (by default) generated code appears. --- test.cs is automatically included in the project, so the generated classes are immediately available. - VladD
  • @VladD, pastebin.com/yQVpXuDG here it appeared. - Ssss
  • @sss: Everything works for me (i) < i.imgur.com/1zRGQXZ.png >, < i.imgur.com/TiQumWQ.png > Did you have the Custom Tool correctly selected? ____ @sss: You probably chose "Run-time template" instead of "Design-time template"? Re-select. - VladD
  • @VladD, yes, there was a preprocessor in the config instead of a generator. Thank. - Ssss