Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject C# code before build

How to inject a new code in existing C# code (not MSIL) before compiling, but not change .cs file?

like image 696
Aleksandr Vishnyakov Avatar asked Dec 01 '25 01:12

Aleksandr Vishnyakov


2 Answers

You can use the Text Template Transformation Toolkit (T4) to handle this type of scenario.

like image 150
Reed Copsey Avatar answered Dec 02 '25 13:12

Reed Copsey


Use T4 templates which write to partial class files. This is the pattern used by Visual Studio--auto-generated code goes in partial files, while your code goes in the main .cs file.