1

In my project I am using custom controls instead of normal ASP.NET controls. We have build an architecture on .NET and are using its controls.

Now I need to write a custom rule to check for if some of windows controls are being used. Reason being, my team needs to be limited to only my custom controls that were designed to replace the windows controls.

Example: I need to search and if they are using System.Windows.Controls.Textbox.....I need it to be an error.

Can anyone please help me out with code?

I hope the problem is clear ..... in case of any further clarifications needed please let me know.

2 Answers2

0

The logic for this sort of rule is fairly simple:

  1. Check method bodies, visiting each constructor invocation to see if the target class inherits from the base Control class.
  2. If it does, verify that the target class is in your namespace or assembly (or however you can best identify it as "yours").

This is relatively simple. A much bigger problem is that the relevant contructors will usually be invoked in designer-generated code, which most folks tend to prefer ignoring when executing FxCop. To get your rule to work, you will need to include designer-generated code in your analyses.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
0

The tool NDepend let's write custom code rules on .NET code much more easily than with FxCop. Disclaimer: I am one of the developer of the tool

With this tool you can write custom code rules over LINQ queries (what is named CQLinq). For example, the query you are asking for can be written this way with CQLinq:

// <Name>Don't use system controls</Name>
warnif count > 0

let systemControls = ThirdParty.Types.Where(
          t => t.DeriveFrom("System.Windows.Forms.Control".AllowNoMatch()))
where systemControls.Count() > 0

from t in systemControls 
let methodsThatCreateT = t.TypesUsingMe.ChildMethods().Where(m => m.CreateA(t))
select new { t, methodsThatCreateT }

While editing such code rule, instantly a browsable result is shown (in 3 milliseconds here). Double clicking any type or method in this result, jumps to its declaration in source code in Visual Studio:

CQLinq don't use system controls

200 default code rules are proposed. The tool is 100% integrated in Visual Studio 2012, 2010, and 2008. Default or custom code rules can be validated inside Visual Studio, and/or at Build Process time, in a generated HTML+javascript report.

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92