views:

467

answers:

2

I'm interested in doing a bit of code-generation based on certain files being present in the .csproj. What extensibility methods are available to me that I could generate a .cs file that would be compiled along with my project?

Caveat: I immediately thought of using T4 templates for the task. However, this solution must be supportable on Visual Studio C# Express. I believe that the express version doesn't support T4 templates

+4  A: 

On C# Express the answer is simple: none.

C# Express does not support extensions. People have tried before and it has got ugly. Lawyers, mess, everything. The famous example is TestDriven.NET.

If you don't mind building at the command line, you could use an msbuild custom task, or a pre-build / post-build command.

In VS2005/VS2008 ("proper"), a "custom tool" is the one of the ways to go; this involves writing a Package; I've done this recently, and the code is open for inspection.

T4 is another option going forward. It didn't fit my needs, but it might do yours.


As a quick check of build events, I've added (in an express project) pre-build and post-build events (Project Properties -> Build Events) of:

echo $(TargetPath)

and

echo $(SolutionDir)

respectively; hit build, and the output is now:

------ Build started: Project: ConsoleApplication10, Configuration: Release Any CPU ------
echo D:\SomePath\ConsoleApplication10.exe
D:\SomePath\ConsoleApplication10.exe
c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /unsafe+ /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Design.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\ConsoleApplication10.exe /target:exe Form1.cs Form1.Designer.cs Form2.cs Form2.Designer.cs Program.cs Properties\AssemblyInfo.cs

Compile complete -- 0 errors, 0 warnings
ConsoleApplication10 -> D:\SomePath\ConsoleApplication10.exe
echo D:\SomePath
D:\SomePath
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

So it seems to work fine (i.e. both events have been executed). Not elegant, but workable. The macros should allow you (with some pokery) to access project items, for example $(ProjectPath)SomeFile.xml

Marc Gravell
does C# express support pre/post build steps?
Joel Martinez
I've just checked, and it seems to; Properties -> Build Events > Pre-build / Post-build. Of course, then you have to do everything via an external batch / exe
Marc Gravell
Thanks for that echo thingy. Might be useful - got to remember.
Arnis L.
+2  A: 

You can open the csproj file (its just an msbuild script) and add in custom targets as well.

There are two default targets provided for you to override for just such a purpose, BeforeBuild and AfterBuild (might be more but I'm unsure of the others).

You can also include custom dlls with new msbuild tasks, though this will generate a security warning when opening the project unless you add the dll to msbuild's SafeImports list ( HKLM\Software\Microsoft\Visual Studio\9.0\MSBuild\SafeImports ), 8.0 if you're using VS2005.

KeeperOfTheSoul