views:

475

answers:

3

(This question is rather similar, but the only answer does not seem to be answering my needs).

I am thinking of using Thrift in C#, and am considering how exactly the build process would work. Do C# projects in Visual Studio 2008 support custom build actions that generate C# classes?

I found the "Custom Tool" option, but I'm not sure it's what I'm looking for ... it only allows design-time usage (not integral to the build process, but rather right-click "Run Custom Tool").

Update

Prebuild events that Fionn suggested are indeed suboptimal, as they don't take dependencies into account and prolong the build process. Also, they are managed from a central location instead of per-file.

+1  A: 

What if you simply put your code generation to the pre-build events.

They are found in the "Build Events" section of the Project Properties in Visual Studio.

Also they support some macros which you will see if you use "Edit Pre-build ..." button.

Fionn
This is highly suboptimal since it will affect how VS resolves whether projects that depend on that project should be rebuilt too.
Pasi Savolainen
Indeed suboptimal, I entered this as a non-solution to the question.I did something like this for a C++ project, a long long time ago (I believe it was in VS 6). Why isn't this functionality visible for C#?
ripper234
A: 

MS Build is highly configurable. Visual Studio itself is also very extensible. You can use the CodeDOM to emit classes. Depending on how much time you want to put into customizing VS and the build process, there is very little you cannot do.

JP Alioto
A: 

The simplest and safest option is to include the generation in the "Pre Build Events" as Fionn mentions. Pros: code is always correct, Cons: Slow as this will mean at each compile that you re-generate the source code, and then rebuild all dependencies, as the generated code as altered.

Another option is to manually regenerate the code files, and if you have a build machine/continuous integration rebuild the code files each build. Pros: Faster builds, if there are few changes Cons: out of date generated code, and hours of wasted debugging trying to solve what's wrong.

Reading the C# project help, it seems that you need a plugin that provides the Custom Tool action you want. But in C++ projects you can define your own custom step MS-Help. So you could develop a VisualStudio plugin (or find one) or add a C++ project to your solution, and add a custom build step to that project, and then include the output in your normal C# project.

The first google for 'visual studio custom tool code gen' has a how-to write your own Custom Tool which would be a good starting point.

Simeon Pilgrim