InitializeOnLoadMethod should tag only static and parameterless methods

Rule ND3206 API Usage / Unity

InitializeOnLoadMethodAttribute initializes an editor static method when Unity loads; RuntimeInitializeOnLoadMethodAttribute does the same after the game has loaded. Both can only tag static parameterless methods.

How to fix

Make the matched methods static and parameterless.

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>InitializeOnLoadMethod should tag only static and parameterless methods</Name>
// <Id>ND3206:InitializeOnLoadMethodShouldTagOnlyStaticAndParameterlessMethods</Id>
warnif count > 0 
from m in Application.Methods where 
 (m.HasAttribute ("UnityEditor.InitializeOnLoadMethodAttribute".AllowNoMatch()) ||
  m.HasAttribute ("UnityEditor.RuntimeInitializeOnLoadMethodAttribute".AllowNoMatch())) &&
 (m.NbParameters > 0 || !m.IsStatic)
select new { 
  m, 
  Debt = 3.ToMinutes().ToDebt(),
  Severity = Severity.Medium
 }

//<Expl>
// The *{0}* is tagged with *InitializeOnLoadMethodAttribute* 
// that should only tag static and parameterless methods.
//</Expl>

//<Desc>
// *InitializeOnLoadMethodAttribute* initializes an editor static method
// when Unity loads; *RuntimeInitializeOnLoadMethodAttribute* does the same
// after the game has loaded. Both can only tag static parameterless
// methods.
//</Desc>

//<HowToFix>
// Make the matched methods static and parameterless.
//</HowToFix>

API Usage / Unity: all 9 items