Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Rule in FxCop to only apply to methods called by particular type's method
    primarykey
    data
    text
    <p>I want to create an FxRule that applies a method, only if the method gets called from a specific class.</p> <p>NOTE: I don't want to just apply a rule to a method of a particular class, i want to be able to handle methods calling other methods calling other methods which do the boxing.</p> <p>I'd like to get FxCop to report problems associated with the method that does the boxing.</p> <p>Below is the code i have so far:</p> <pre><code>using System; using System.Linq; using Microsoft.FxCop.Sdk; using System.Collections.Generic; class CheckUpdatableComponents : BaseIntrospectionRule { private string[] MethodsToCheck = 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(string namespaceName, TypeNodeCollection types) { foreach (var type in types.Where(T =&gt; IsSubClassOf(T, "Microsoft.Xna.Framework.Game"))) { foreach (var MethodToCheck in MethodsToCheck) { Method RunMethod = type.GetMethod(Identifier.For(MethodToCheck)); if (RunMethod != null) { Visit(RunMethod); } } } return Problems; } public override void VisitMethod(Method method) { Problems.Add(new Problem(GetResolution(), method, method.ToString())); // This problem only appears for each of the RunMethods, and doesn't seem to be recursing down the tree. 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) { } } base.VisitMethod(method); } 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> <p>My issue with the above code, is firstly, that it doesn't appear to be recursing. Secondly, that FxCop reports the problems as being associated with the namespace (probably because i kick off the visit using the Check(namespace....) part.</p> <p>My problem is that i want FxCop to report a method that has boxing as a problem, but only if it gets called by a particular method, however i have no way of walking up the call tree, i can only visit lower nodes to check for problems with my starting location.</p> <p>Has anyone done this sort of thing before?</p> <p>How can i find out what methods call a given method?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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