Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change modified classes generated by T4

Tags:

t4

I used T4 to generate some entity classes , but I forgot to make them Serializable. So is there any solution to use something like T4 to add a Serializable attribute to all my classes ?

like image 839
Ali Foroughi Avatar asked Mar 03 '26 23:03

Ali Foroughi


1 Answers

If you've already modified your generated classes, I think you might find it easier to do with Visual Studio's global replace with a fancy regex to find the classes you need to change. (If that's not possible, it's not hard to write a quick console application to process the files).

Using T4 you can control which files are overwritten, for instance using the Output.PreserveExistingFile which comes with T4 Toolbox.

<#
  var t = new SampleTemplate();
  t.Output.File = "Sample.cs";
  t.Output.PreserveExistingFile = true;
  t.Render();
#>

And then you can delete the specific files you want recreated. But however you determine which files to overwrite, any changes to those files that you've made since last regenerating will be lost. One recommendation is to build your templates as partial classes so that you can put all manual modifications in a separate file (but that doesn't really help you if you've already modified your generated classes).

like image 153
shamp00 Avatar answered Mar 06 '26 03:03

shamp00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!