I have a projet in c# .net.
I need to hook the “pre build” event so I can run a c# class that will generate other c# class to the same project.
I know that in java you can generate source code using maven plugin, is it possible in .net?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are at least two options:
T4 is the inbuilt way to do code generation (and widely used in ASP.NET MVC scaffolding and Entity Framework type generation). However T4 doesn’t directly support compile time generation. It does either design time – with the developer triggering (re-)generation – or runtime generation; (third party tools can add compile time generation).
Adendum (after 6 years): NuGet packages like Clarius.TransformOnBuild can also do this.
A custom MSBuild task (including running an external program) can be used to create code which is then included in the compile. In the simplest cases the “pre-build” setting of a project can be used (the Build Events tab of the project properties). Directly working in MSBuild (and a
.csprojis a MSBuild file) gives more flexibility.