Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT:</strong> This doesn't work in the case of virtual method calls where the IL is <code>callvirt</code>. See my question <a href="https://stackoverflow.com/questions/6454181/how-to-get-the-method-actually-called-by-the-callvirt-il-instruction-within-fxcop">here</a>.</p> <p>I managed to work it out, by discovering the method <code>CallGraph.CallersFor()</code>. I'm now looking for methods declared with a given attribute, or declared by a class with a given attribute, but the principal is the same.</p> <pre><code>using System; using System.Linq; using Microsoft.FxCop.Sdk; using System.Collections.Generic; class CheckUpdatableComponents : BaseIntrospectionRule { // private string[] MethodsToCheckNames = new string[] { "BeginDraw", "BeginRun", "Draw", "EndRun", "EndDraw", "Update" }; /// &lt;summary&gt;Gets the base class hooked up.&lt;/summary&gt; public CheckUpdatableComponents() : base("CheckUpdatableComponents", "FxCopRules.Rules", typeof(CheckUpdatableComponents).Assembly) { } public override ProblemCollection Check(Member member) { Method method = member as Method; if (method != null) { if (ShouldCheckMethod(method)) { foreach (var Instruction in method.Instructions) { if (Instruction.NodeType == NodeType.Box || Instruction.NodeType == NodeType.Unbox || Instruction.NodeType == NodeType.UnboxAny || Instruction.OpCode == OpCode.Box || Instruction.OpCode == OpCode.Unbox || Instruction.OpCode == OpCode.Unbox_Any) { Problems.Add(new Problem(GetResolution(), Instruction, Instruction.SourceContext.StartLine.ToString())); } } } } return Problems; } public bool ShouldCheckMethod(Method method) { Queue&lt;Method&gt; MethodsToCheck = new Queue&lt;Method&gt;(); List&lt;Method&gt; MethodsChecked = new List&lt;Method&gt;(); MethodsToCheck.Enqueue(method); while (MethodsToCheck.Count != 0) { Method MethodToCheck = MethodsToCheck.Dequeue(); if (!MethodsChecked.Contains(MethodToCheck) &amp;&amp; MethodToCheck != null) { /*if (IsSubClassOf(MethodToCheck.DeclaringType, "Microsoft.Xna.Framework.Game") &amp;&amp; MethodsToCheckNames.Contains(MethodToCheck.Name.Name)) { return true; }*/ foreach (var attribute in MethodToCheck.Attributes.Union(MethodToCheck.DeclaringType.Attributes)) { if (attribute.Type != null &amp;&amp; attribute.Type.FullName == "GridEngine.Components.Debugging.Attributes.FxCop.PerformanceCriticalAttribute") { return true; } } // Add methods up the class tree MethodsToCheck.Enqueue(MethodToCheck.OverriddenMethod); MethodsToCheck.Enqueue(MethodToCheck.HiddenMethod); // Add calling methods foreach (var CallingMethod in CallGraph.CallersFor(MethodToCheck)) { MethodsToCheck.Enqueue(CallingMethod); } } MethodsChecked.Add(MethodToCheck); } return false; } private bool IsSubClassOf(TypeNode type, string typeName) { if (type.FullName == typeName) return true; if (type.BaseType == null) return false; else return IsSubClassOf(type.BaseType, typeName); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload