4

I'd like to create a targets file that is compatible with C# and VB projects, unfortunately it needs knowledge of the project language.

I could just make one file for each language, but it would be cleaner if I could just use one file, and have a condition that checks which language we need.

Is there a way of doing this?

I was hoping for something like this:

<Target Name="BeforeBuild" Condition="'$(Language)"' == 'C#'">

or

<Target Name="BeforeBuild" Condition="'$(ProjectFile).Extension' == 'csproj'">

I've tried searching, but there is a lot of noise for these search terms. I've tried many variations on the following terms with no luck.

  • msbuild project check language
  • msbuild project condition if vb or c#
  • csproj check language

If we get a good answer, hopefully the next person will find this a lot easier to find an answer for.

Doctor Jones
  • 21,196
  • 13
  • 77
  • 99
  • Could maybe checking for the specific `` work? There should be either `` or `` imported. – Filburt Oct 29 '20 at 12:51
  • @Filburt how do you check for an import? – Doctor Jones Oct 29 '20 at 13:04
  • Still trying to figure that out myself. – Filburt Oct 29 '20 at 13:09
  • 1
    Interesting one. Looks like you've found the Language property (I see it in output when using /v:diag), Running MSBuild with /pp:C:/out.txt (see -preprocess[:file]) on an ASP.NET C# project shows it coming down from in Microsoft.CSharp.CurrentVersion.targets. I see it used like true in Microsoft.CodeAnalysis.targets. So it looks like you're on the right track. – Anthony Oct 29 '20 at 20:48

0 Answers0