Using T4 Templates to Generate Runtime
You choose this method if you need to generate code at runtime. For example, you want to generate a page object using Selenium.

Create a folder in your solution, name it Templates (good name for T4 Templates).
Then add a new element, type T4, then select Temporary text template .... We named our template MyNodeName.tt, which is shown in the image above.
Add your code as shown below, the upper part was inserted by Visual Studio ...

You can see what we want to pass in the namespace and ClassName (this is the markup Model.NameSpaceName and Model.ClassName seen above.
The hard part is learning how to pass parameters ...
Create a new CS class named partial in the file name.

But in the class do not call this name MyNodeNamePartial this is the name MyNodeName:
public partial class MyNodeName { public MyNodeNameModel Model { get; set; } }
This is the same name as the TT file. (MyNodeName), which creates its own partial class. But now notice that we have a MODEL value for this class type.
public class MyNodeNameModel { public MyNodeNameModel() { ClassName = "Test"; } public string ClassName { get; set; } public string NameSpaceName { get; set; } }
The model class contains ClassName and NameSpaceName and all that you want to "insert" into the template.
The key to work shown is that a text runtime template was used! If you use a text template, no matter what you do, you will see errors similar to โModel not foundโ or other controversial problems.
Debugging Tips: โThe model cannot be foundโ is the T4 generation code telling you that in your partial class with the MODEL variable that it cannot find! Check both partial and model types to make sure they are in the same namespace as any other namespace of regular classes, if created in this folder.
John peters
source share