If so, for what purpose?
Yes. We commonly use it to take away the need for the grunt work in CRUD operations on a database.
This will probably be more a legacy approach though as we migrate to more modern ORMs such as LINQ.
A great deal of features in C# use code generator, for example the WinForms designer and the LINQ to SQL designer, so a lot of C# developers use code generators even if they don't know about it!
I think it's good for scenarios where you would be writing grunt copy+paste code hat you're more likely to screw up than an automated process.
I use SubSonic to create my Data Access Layer, and the reasons are on this page (not my blog):
http://www.bloodyflux.com/post/2007/10/5-Reasons-why-SubSonic-is-Better-than-Sliced-Bread.aspx
Code generation is great for automation. Someone has asked for preprocessor alternatives in C#. Code generation is definitely an answer.
I also use code generation when writing parsers. Although I almost always use hand-coded recursive descent parsers and do not rely on parser generator tools, code generation can still help by creating the necessary infrastructure (e.g. the handling of keywords is often very similar, and token classes have almost the same layout). I'm mostly using Ruby scripts to create the relevant code.
Another instance of code generation I've used was a dynamic web application that was written in PHP but was generated using another application. The web application had a static framework and a dynamic part that depended on the data entered by the user. This dynamic part was generated from C# using the CodeDom
infrastructure and a hand-coded CodeGenerator
that created the PHP code from templates.
/EDIT: I've highlighted the different uses of code generation.
We do it a lot where I work to generate unit tests. Our tests are basically low level assembly, and we use code generators to generate random sequences to stress various parts of the hardware.
Also using Subsonic to autogenerate the DAL. I run the code-gen automatically when I open my VS solution, and I have a toolbar button for a quickie. The whole DAL is generated in about 9 - 10 seconds max.
Can't think of a bigger productivity jump than when we decided to code-gen our DAL. I'm now seriously considering code generation for middle tiers and I would even code-gen our UI if possible,
I have a series of common lisp macros that read in simple meta data and generate DALs and even simple data entry forms in c#. It saves oodles of time. Don't fall in the trap of trying to automate every case as you quickly hit the law of diminishing returns and end up writing your own programming language.
Yes, I use it.
1.- Implementing DAOs that have methods like: save(User), save(Invoice), List findAllByName(String name)... Sometimes by hand. Sometimes with CGLIB.
2.- Implementing javabean mappers like: tons of classes that implement some interface like interface Mapping<S, T> { T map(S s); }
. Always with CGLIB.
3.- Implementing getters, setters and methods like addPropertyChangeListener() for JavaBeans. Always with CGLIB.
4.- Someday I will generate JSP tags. If (I'm lucky) { with CGLIB } else { by hand }.