|
|
How to ignore compiler-generated members? |
|
Author |
Message |
AlexGold
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Our company has rule to prefix all class member variables with ‘m_’. When I try to make a custom FxCop rule, it will pick up all compiler-generated controls’ names (starting with underscore) in the web application (VB). How can I make it so custom rule will ignore them
Thanks...
Visual Studio Team System44
|
|
|
|
|
Jeffrey van Gogh - MSFT
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Hi Alex,
if the locals you're talking about are compiler generated and not tool generated, RuleUtilities.IsCompilerGenerated(Local) should work. Otherwise you probably will have to write a small helper that determins if a variable is generated.
Regards,
Jeffrey
|
|
|
|
|
AlexGold
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Jeffrey,
Thanks for your replay, but I'm not sot sure I understand what you mean by 'tool generated'. AFAIK, VB uses the underscore as a prefix on compiler generated fields for WithEvents Variables. That is, if we have a text box TextBox1 on the form (Windows or web): Private WithEvents TextBox1 as TextBox VB will generate a TextBox1 property and a backing field _TextBox1.
I want to "weed out" these backing fields and check only for developer - declared backing fields. If I understand right, they are not locals, so RuleUtilities.IsCompilerGenerated wouldn't be help here...
Thanks again,
Alex
|
|
|
|
|
Anton Papst
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Hello Alex,
You are right. RuleUtilities.IsCompilerGenerated(Local) will help only by locals which were generated by compiler while compiling the source code. In your case the variables/fields are generated on the source code level, so they will remain with the same names on the IL-Code level and RuleUtilities.IsCompilerGenerated(Local) method will not work.
You could search for all fields which start with prefix "_" and look if there are other fields in the same class which have the same names but without "_".
Kind regards,
Anton Papst.
|
|
|
|
|
AlexGold
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Anton,
Would you be so kind to give me advice on how to do it In ildasm I can see that generated _TextBox1 is a private class and the TextBox1 is an instance class. But I cannot figure out how to do the check you were reffering to. Whatever I try doesn't work.
Any help is greatly appreciated. Thanks,
Alex
|
|
|
|
|
Anton Papst
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Alex,
VB is not my world, so I don't know how the compiler generated source code with _TextBox1 look's like, but if the
generated _TextBox1 is a private class field you could try it the following way:
public override ProblemCollection Check(Class c)
{
ArrayList surely_not_compiler_generated = new ArrayList();
MemberList m_list = c.Members;
//First run.
foreach (Member mem in m_list)
{
Field fld = mem as Field;
if (fld == null)
continue; //return null;
if (!fld.Name.Name.StartsWith("_") && !fld.IsPrivate)
surely_not_compiler_generated.Add(fld);
}
//Second run.
foreach (Member mem in m_list)
{
Field fld = mem as Field;
string fld_name = fld.Name.Name;
if (surely_not_compiler_generated.Contains(fld) ||
(fld.Name.Name.StartsWith("_") && !isDesignerGenerated(surely_not_compiler_generated, fld)))
{
//Do the work.
}
}
return Problems;
}
private bool isDesignerGenerated(ArrayList fields, Field current_field)
{
string current_field_without_prefix = current_field.Name.Name.Substring(1,current_field.Name.Name.Length-1);
foreach (Field f in fields)
{
if (f.Name.Name.Equals(current_field_without_prefix) &&
f.Type == current_field.Type)
{
return true;
}
}
return false;
}
Kind regards,
Anton.
|
|
|
|
|
AlexGold
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Anton,
Thanks for the sample code. The idea is very bright, and I'm trying to make it work for me. The only problem is that as test runs show, neither _TextBox1 nor TextBox1 (and non of the user - declared private fields) are part of the c.Members (MemberList m_list = c.Members;). So, the question is how can I get the collection of all members of the field's parent class (or even collection of members of the assembly)
Thanks again,
Alex
|
|
|
|
|
Anton Papst
|
Posted: Visual Studio Code Analysis and Code Metrics, How to ignore compiler-generated members? |
Top |
Alex,
it seems that your private members are note checked by FxCop now. First, set the field: "Project->Options->Spelling & Analysis->Run all overridable rules against all targets". It should help. Otherwise you can try to override the TargetVisibility function:
public override TargetVisibilities TargetVisibility { get { return TargetVisibilities.(choose apropriate mode) ; } }
Kind regards,
Anton.
|
|
|
|
|
|
|