T4 - TT - use custom classes in TT files

I would like to use my own class definition in the CS file in my TT.

Example:

public class ClassDefinition { public string NameSpace { get; set; } public string Name { get; set; } public string Protection { get; set; } List<ClassProperty> Properties { get; set; } } 

My TT looks like this:

 <#@ template debug="true" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> <#@ assembly name="System" #> <#@ assembly name="System.Core" #> <#@ assembly name="System.Xml"#> <#@ import namespace="System.Xml" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.IO" #> <#@ include file="$(ProjectDir)ClassDefinition.cs" #> <# // Read the model file XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(this.Host.ResolvePath("GeneratedXmlFile.xml")); IList<XmlNode> nodeList = new List<XmlNode>(); foreach (XmlNode node in doc.DocumentElement) { switch(node.Name) { case "Model": { ClassDefinition classDefinition = new ClassDefinition(); 

But I have this error message:

 Compiling transformation: The type or namespace name 'ClassDefinition' could not be found (are you missing a using directive or an assembly reference?) 

I checked on the Internet and tried: - use - use assembly - use USE But nothing works.

Any ideas?

+6
source share
4 answers

Here is the complete solution:

1) Divide the classes into another project 2) Include a link to these classes through TT through

 <#@ assembly name="$(TargetDir)MyOwnLibraryProject.dll" #> <#@ import namespace="MyOwnNamespace" #> 

3) Remember to include a link to this library in your TT project

4) You need to copy the file MyOwnLibraryProject.dll to the BIN \ DEBUG folder of the TT solution

5) The magic appears !!!

Every time you change your DLL, do not forget to put the new version in the folder :) Or just configure the output of the library project the same as your TT. I would like to thank all of you for providing guides and ideas.

+7
source

If you understand correctly, you are trying to reuse the class as part of the template generation.

This class should be in the tt file itself, the assembly action is set to none, the custom tool is nothing. I have a template manager class with the following at the top:

 <#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Diagnostics" #> <#+ public class TemplateManager { 

Then in other t4 templates I use:

 <#@ include file="TemplateManager.tt"#> 

and then

 List<Values> values = TemplateManager.PrepareVariables(code, container, itemCollection.OfType<EntityType>()) 

In your case, this ClassDefinition.tt file will contain:

 <#@ template language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Diagnostics" #> <#+ public class ClassDefinition { public string NameSpace { get; set; } public string Name { get; set; } public string Protection { get; set; } List<ClassProperty> Properties { get; set; } } #> 

Then you can turn on

 <#@ include file="ClassDefinition.tt"#> 
+4
source

Including a C # file in a T4 template using:

 <#@ include file="$(ProjectDir)ClassDefinition.cs" #> 

Add text to the output of the T4 template. It does not compile the class.

You have debug = true set in your T4 template so that you can see what T4 generates if you look in your% TEMP% directory. When you run your T4 template, you will see the .cs file generated in the TEMP directory. In this file you will have something similar to:

  this.Write("public class ClassDefinition\r\n{\r\n public string NameSpace { get; set; }\r\np" + "ublic string Name { get; set; }\r\n public string Protection { get; set; }\r\n\r\n " + " List<ClassProperty> Properties { get; set; }\r\n}"); 

So, all that happens to your C # class is that it will be written to the generated T4 output.

What you probably want to do is include the ClassDefinition.cs file in your project so that it is compiled as part of your project. You can then reference the assembly, which includes the ClassDefinition class. Therefore, if your project is MyLibrary.dll, which contains the ClassDefinition.cs compilation, you should be able to use:

 <#@ assembly name="$(SolutionDir)$(OutDir)MyLibrary.dll" #> 

The line containing the ClassDefinition.cs file must be deleted.

+1
source

Adding this parameter after import operations should be performed:

 <# ClassDefinition cd = new ClassDefinition(); #> 
0
source

Source: https://habr.com/ru/post/972837/


All Articles