Don't use System.Reflection in performance critical messages

Rule ND3208 API Usage / Unity

System.Reflection is slow: don't use it in performance critical messages like Update, FixedUpdate, LateUpdate, or OnGUI.

How to fix

Don't use reflection in matched methods.

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>Don't use System.Reflection in performance critical messages</Name>
// <Id>ND3208:DontUseSystemReflectionInPerformanceCriticalMessages</Id>

warnif count > 0 

let reflectionMembers = Namespaces.WithName("System.Reflection").ChildMembers()

from m in Application.Methods.UsingAny(reflectionMembers) where 
 m.ParentType.DeriveFrom("UnityEngine.MonoBehaviour".AllowNoMatch()) &&
 m.SimpleNameEqualsAny("Update", "FixedUpdate", "LateUpdate", "OnGUI")

let shouldntUse = m.MembersUsed.Intersect(reflectionMembers)
select new { 
  m,
  shouldntUse,
  Debt = (5 + 5*shouldntUse.Count()).ToMinutes().ToDebt(),
  Severity = Severity.Medium
}

//<Expl>
// The *{0}* shouldn't use *{1}* because 
// *Reflection* shouldn't be used in performance critical messages.
//</Expl>

//<Desc>
// *System.Reflection* is slow: don't use it in performance critical
// messages like *Update*, *FixedUpdate*, *LateUpdate*, or *OnGUI*.
//</Desc>

//<HowToFix>
// Don't use reflection in matched methods.
//</HowToFix>

API Usage / Unity: all 9 items