Skip to main content

Analyzers

Roslyn analyzers check MVVM code at compile time and offer automatic fixes.

Contents


Overview

Aspid.MVVM ships two analyzer sets:

PackagePurpose
Aspid.MVVM.AnalyzersChecks ViewModel and View code
Aspid.MVVM.Unity.GeneratorsUnity-specific checks

The analyzers run in the IDE (Rider, Visual Studio, VS Code) and during Unity compilation.


Diagnostics

ViewModel

IDSeverityDescription
AMVVM001WarningA class with [ViewModel] must be partial
AMVVM002WarningA field with [Bind] must be inside a [ViewModel] class
AMVVM003WarningA method with [RelayCommand] must be inside a [ViewModel] class
AMVVM004InfoPrefer the generated property over the backing field
AMVVM005WarningThe CanExecute method/property was not found
AMVVM006WarningCanExecute parameters do not match the command

View

IDSeverityDescription
AMVVM010WarningA class with [View] must be partial
AMVVM011WarningA class with [View] must inherit MonoView
AMVVM012InfoA binder field has no matching ViewModel property

Code fixes

Most diagnostics come with an automatic fix:

DiagnosticCode fix
AMVVM001Add partial to the class
AMVVM004Replace _field with Property
AMVVM010Add partial to the class

Installation

The analyzers ship with the Aspid.MVVM package. If you cloned from source:

git submodule update --init --recursive

Make sure the Aspid.MVVM.Analyzers submodule is initialized.


Configuration

Disabling specific diagnostics

In .editorconfig:

[*.cs]
# Turn off the property-over-field suggestion
dotnet_diagnostic.AMVVM004.severity = none

# Lower to a suggestion
dotnet_diagnostic.AMVVM001.severity = suggestion

In code (for single places)

#pragma warning disable AMVVM004
var text = _text; // Using the backing field on purpose
#pragma warning restore AMVVM004

See also