[.NET 2.0] Playing Around with BuildProvider and CodeDom

This is old news for some of you, but I've never looked at the ASP.NET buildprovider functionality in more detail until tonight, and it is pretty cool. The buildprovider can generate/build code from certain file types you drop into an ASP.NET project and I'm sure you're familiar with .xsd files and how VS.NET generates classes from those. The cool thing is that you can write your own buildprovider and generate code yourself.

If you think this sounds like fun, here's a few links for you to get you started:

What you need to know though, is that if you need to do something more elaborate, the CodeDom stuff isn't a walk in the park. Also, the buildprovider only works for ASP.NET projects, which is sad news, but Scott Guthrie explained it in an old blog post of his:

In the beginning of the Whidbey project there was a plan to enable broad declarative support for file-types as compiler inputs (for example: XAML could have been processed this way).  Unfortunately due to time/resources the compiler teams had to scale back their support for this -- and only web projects still contain them.  I'm hoping we'll see broader support for them in all project types with future releases.

Buildproviders should also work in Web Application Projects, but I couldn't get it to run... Have to look at that again some other day. I saw Scott Guthrie mention this in some blog post, but I cannot find it now. UPDATE: I got a mail from Scott about this:

Unfortunately build providers don’t work directly with web application projects (they still work for the running app – but the assemblies they create aren’t linked to by the web application project assembly, so you can’t reference the types directly.

You can, however, use the VS feature called “single file generators” that enable you to generate .vb/.cs files from declarative types, and then include this within your project assembly.

Pity... guess I have to look at single file generators then if I want to follow up on this one. Or just run an external code generator manually. No time for this now anyway, too much work to do...

No Comments