Archives

Archives / 2016 / September
  • How to Disable Warnings in Generated C# Files of UWP Apps

    The first time I moved my UWP development beyond writing “throwaway code for learning purposes”, I did what I always do when starting a new “real” project: On the “Build” tab of the project properties, I switched the setting “Treat warnings as errors” to “All” and set the checkmark at “XML documentation file”.

    Nasty surprise: When compiling the UWP app, the compiler stumbled over missing XML doc comments in code that was generated in the background.

    Fortunately, I quickly found a solution on StackOverflow that automatically added a #pragma warning disabled to the generated files using MSBuild. That worked well for me until recently, when I created a new solution configuration that had a name containing spaces. Digging a bit into MSBuild, and with the help of another StackOverflow answer, I was able to figure out the following build target (which I’ll add as an answer to the original StackOverflow question):

    <Target Name="PragmaWarningDisablePrefixer" AfterTargets="MarkupCompilePass2">
    	<ItemGroup>
    		<GeneratedCSFiles Include="**\*.g.cs;**\*.g.i.cs" />
    	</ItemGroup>
    	<Message Text="CSFiles: @(GeneratedCSFiles->'&quot;%(Identity)&quot;')" />
    	<Exec Command="for %%f in (@(GeneratedCSFiles->'&quot;%(Identity)&quot;')) do echo #pragma warning disable &gt; %%f.temp &amp;&amp; type %%f &gt;&gt; %%f.temp &amp;&amp; move /y %%f.temp %%f" />
    </Target>
    

    How to use this:

    • Unload and edit your “.csproj” file in Visual Studio (or open it in Notepad if Visual Studio is closed)
    • Copy-and-paste the task just before the closing </Project> tag
    • Save and (re)open in Visual Studio