walbacal

I'm doing my own FxCop rules for checking my class, methods and atributtes naming

I have two questions again:

1 - Checking the fields on my class I can know the type name and namespace fo them, but if I want to check the name of every single class in my project, how can I get it For example if I have a form like this:

MyForm1 : System.Windows.Forms.Form
{
}

and I want all my form with an specific name format, for example: beginning with Form
How can I check the class name without cheking it through their fields How can I know the classes of which it inherits


2 - If I declare an interface I check their member and class name like a normal class How can I know if a declaration is a delegate, a class, an interface, an enum or others

Thanks for your help


Re: Visual Studio Code Analysis and Code Metrics Check my class declaration on my own rules

spano

1- You have to override in your custom rule que Check method that takes a TypeNode as a parameter (take a look at the BaseIntrospectionRule members to see all overloads). For your example:

public override ProblemCollection Check(TypeNode type)
{
if (! type.Name.Name.StartsWith("Form")){
Resolution resolution1 = base.GetResolution(new String[1] { type.Name.Name });
Problem problem1 = new Problem(resolution1);
base.Problems.Add(problem1);
}
return base.Problems;
}

You can see which class the type inherits through the TypeNode.IsDerivedFrom method or the BaseType property.

2- Yes, you manage it like any other type.

You can check for an interface like this:
public override ProblemCollection Check(TypeNode type)
{
Interface myInterface = type as Interface;
if (myInterface != null){
//your logic
}
return base.Problems;
}


Look in Microsoft.Cci namespace, you hace the Event and EnumNode deriving from TypeNode too.






Re: Visual Studio Code Analysis and Code Metrics Check my class declaration on my own rules

walbacal

I have a problem yet. There is another way to test my rules without including it in a FxCop project
But this is not me real problem. It seems to me that my rule never check my Check(TypeNode type) method for the interface. This is the my code:

public override ProblemCollection Check(TypeNode type)
{
Interface interfaz = type as Interface;

if (interfaz == null)
return null;

string prefijo = "I";
string sufijo = "z";
//if (!interfaz.Name.Name.StartsWith(prefijo) || !interfaz.Name.Name.EndsWith(sufijo))
{
Problems.Add(new Problem(GetNamedResolution("Interfaz", interfaz.Name.Name, prefijo, sufijo), interfaz));
}
return base.Problems;
}

And in my interface dll I only have:

namespace WindowsApplication1
{
public interface Prueba
{
void Metodo();
}
}

What I'm doing wrong Thanks for your felp




Re: Visual Studio Code Analysis and Code Metrics Check my class declaration on my own rules

David M. Kean - MSFT

Have you overridden the TargetVisibility property on BaseInstropectionRule If so, can you paste it's contents

Have you tried debugging the rule to see if FxCop actually calls the Check method at all

To debug a rule:

  1. Create a new FxCop project
  2. Add the target you want to analyze
  3. Add your custom rule, making sure that the *.pdb are alongside the assembly
  4. Open your solution with the custom rule
  5. Attach the debugger to FxCop (Debug -> Attach to Process)
  6. Set a break point in your rule
  7. Analyze in FxCop

Regards

David






Re: Visual Studio Code Analysis and Code Metrics Check my class declaration on my own rules

walbacal

Thank you very much. That was my mistake and I'm debugging perfectly now