Question

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 ?

Was it helpful?

Solution

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).

OTHER TIPS

Are those generated classes partial classes? If so, use another T4 template in order to generate a partial class definition decorated with the Serializable Attribute.

Otherwise you could use the Visual Studio CodeModel in order to identify all classes that need this implementation inside another T4 template and then let this T4 template add the code fragments necessary.

If you are using tangible's T4 Editor, it comes with a free Template Gallery and as far as I know there is a template called "Add NotifyPropertyChanged" which does pretty much what you are looking for: discovering code classes inside a Solution and making them implement a given interface. You might adapt that one easily and get your desired functionality.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top