Classes tagged with InitializeOnLoad should have a static constructor

Rule ND3200 API Usage / Unity

InitializeOnLoadAttribute initializes an Editor class when Unity loads and when scripts are recompiled; the tagged class's static constructor is responsible for the initialization.

How to fix

Create a static constructor for the matched classes.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Classes tagged with InitializeOnLoad should have a static constructor</Name>
// <Id>ND3200:ClassesTaggedWithInitializeOnLoadShouldHaveAStaticConstructor</Id>
warnif count > 0 
from t in Application.Types where t.HasAttribute("UnityEditor.InitializeOnLoadAttribute".AllowNoMatch())
where !t.Name.Contains("Readme") && // Discard false positive on generated code
       t.Methods.SingleOrDefault(m => m.IsClassConstructor) == null
select new { 
  t, 
  Debt = 8.ToMinutes().ToDebt(),
  Severity = Severity.Medium
 }

//<Expl>
// The *{0}* is tagged with *InitializeOnLoadAttribute* and thus, 
// should have a static constructor to initialize the *Editor* class.
//</Expl>

//<Desc>
// *InitializeOnLoadAttribute* initializes an Editor class when Unity loads
// and when scripts are recompiled; the tagged class's static constructor is
// responsible for the initialization.
//</Desc>

//<HowToFix>
// Create a static constructor for the matched classes.
//</HowToFix>

API Usage / Unity: all 9 items