Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code Generation, project-based content

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

like image 401
Joel Martinez Avatar asked Aug 24 '26 12:08

Joel Martinez


1 Answers

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

like image 87
Marc Gravell Avatar answered Aug 26 '26 02:08

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!