问题
We have a coding standard that says all shared (static) fields and methods must be called with the class name. E.g.
NameOfClass.whatever
Rather then
whatever
Is there a tool that we can use to check this is in fact the case? (Likewise for modules)
Sorry I should have make it clearer we are using VB.NET.
This is a bigger example of what I mean.
Public Class Class1
Public Shared Sub SharedMethod()
End Sub
Public Shared sharedField As Integer
Public Sub NotSharedMethod()
'this next line shold be written as Class1.SharedMethod
SharedMethod()
'this next line shold be written as Class1.sharedField
sharedField = 5
End Sub
End Class
see also What StyleCop like tools are there for VB.NET
回答1:
Yes,
Use StyleCop and write your custom rule to do your check.
Here's a reference to check how to write custom-rule for StyleCop.
回答2:
Sure, just create a custom rule in StyleCop. Then incorporate the use of StyleCop into your automated build process.
Sorry, I did not even realize that StyleCop did not have a VB.NET version. What you need is a static analysis tool for VB.NET. Based on this thread, it looks like Project Analyzer is an option. Unfortunately it's not free.
From the web site:
Maintain. To help maintenance, Project Analyzer lets you enforce coding standards[.]
Whatever tool you use, be sure it incorporate it into your automated build process.
回答3:
In the Project Properties > Compile, you can set the Warning configuration for "Instance Variable access shared member" to Error and it should generate a compiler error instead of a warning.
I'm not sure how you might do it for all projects. You could change the project template to include that option for all new projects.
回答4:
Sorry I never did find a good tools for this.
回答5:
You might be able to use FxCop for this purpose, and write a custom rule. Here is a good site that explains how to write custom FxCop rules.
来源:https://stackoverflow.com/questions/2103579/enforcing-using-the-class-name-whenever-a-shared-member-is-accessed