Exclude CsWinRT-required partial types from MA0204
Merged
CO
2mo ago+183+183+012345678901234567890123456789-2-2-0123456789
MA0204 was flagging partial as unnecessary in multi-targeted MAUI/Windows scenarios where CsWinRT requires the type to remain partial for WinRT AOT/trimming compatibility. This produced contradictory guidance: MA0204 suggested removing partial, while the Windows build required it.
-
Analyzer behavior
- Keep
partialwhen the type may participate in CsWinRT AOT generation. - Detect common WinRT-mapped interfaces (for example
IDisposable, collection interfaces,INotifyPropertyChanged,ICommand) and skip MA0204 for those types. - Preserve
partialon containing types when a nested type requires it.
- Keep
-
Scope of the exemption
- Apply the exemption when CsWinRT support is present in the compilation.
- Fall back to Windows-target detection from analyzer config build properties when CsWinRT types are not directly available.
-
Coverage
- Add focused tests for:
- a
partialclass implementingIDisposable - the non-Windows case, which should still report MA0204
- a containing partial type with a nested CsWinRT-relevant type
- a
- Add focused tests for:
-
Documentation
- Update MA0204 docs to mention the CsWinRT/MAUI Windows exception.
Example:
public partial class CertificateHolder : IDisposable
{
public void Dispose()
{
}
}
With this change, MA0204 no longer suggests removing partial from types like the above when that modifier may be required by CsWinRT on Windows-targeted builds.
- Fixes #1450