How to only compile certain directories depending on target framework like in MAUI? #7782
-
I'm trying to replicate the system that MAUI uses to separate platform / target dependant code without using the pre processor ( In MAUI, the convention is that there is a platform directory that contains each build target and only the relevant code is compiled, thus effectively swapped out for each build. I've checked the csproj file but there seems to be no reference to Is this specifically a MAUI convention, or is there a way to replicate this behaviour in other project types? Ive tried to remove the root directory and then re-add the the relevant sub folder depending on the target framework but roslyn complains about objects being defined multiple times (i.e its seeing files for all target frameworks). <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net5;net6</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Platforms\**" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net5' ">
<Compile Include="Platforms\net5\**" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6' ">
<Compile Include="Platforms\net6\**" />
</ItemGroup>
</Project>
Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
MAUI does this via some fairly complicated, general logic in one of its You can accomplish similar things with a more straightforward |
Beta Was this translation helpful? Give feedback.
MAUI does this via some fairly complicated, general logic in one of its
.targets
files: https://github.com/dotnet/maui/blob/c1439acf4dfeb0e5a6f56583d6a1860a12ff4a6f/.nuspec/Microsoft.Maui.Controls.SingleProject.targets#L72-L119You can accomplish similar things with a more straightforward
Compile Remove="myThing/**"
, though you should be aware of some behaviors in the project system that get confused with justRemove
; I think that's why MAUI does theExcludeFromCurrentConfiguration
thing.