2、在内库项目中引用Microsoft.Cci.dll和FxCopSdk.dll两个dll文件。添加一个类FunctionParametersCheck。继承BaseIntrospectionRule。
'功能: 参数命名请采用Camel命名规范
'创建人: 朱祁林'创建时间:2010-12-07
'修改人: 朱祁林'修改时间:2010-12-07
Imports Microsoft.FxCop.Sdk
Imports System
Namespace MySoftRules
Public Class FunctionParametersCheck
Inherits BaseIntrospectionRule
' Methods
Public Sub New()
MyBase.New("FunctionParametersCheck", "CustomRules.CustomXMLRules", GetType(FunctionParametersCheck).Assembly)
End Sub
Public Overrides Function Check(ByVal member As Member) As ProblemCollection
Dim method As Method = TryCast(member, Method)
If ((Not method Is Nothing) AndAlso (method.DeclaringMember Is Nothing)) Then
Dim i As Integer
For i = 0 To method.Parameters.Count - 1
'true:找到不符合规范的参数,false表示没有找到不符合规范的参数
Dim blnFoundAddString As Boolean = False
Dim strFullType As String = method.Parameters.Item(i).Type.FullName
Dim strType As String = strFullType.Substring((strFullType.LastIndexOf(".") + 1), ((strFullType.Length - strFullType.LastIndexOf(".")) - 1))
Dim strName As String = method.Parameters.Item(i).Name.Name
If strType.ToLower() = "string" Then
blnFoundAddString = CheckField(strName, "str")
ElseIf strType.ToLower() = "int32" Then
blnFoundAddString = CheckField(strName, "int")
End If
If blnFoundAddString Then
Dim resolu As Resolution = MyBase.GetResolution(method.ToString, strName)
MyBase.Problems.Add(New Problem(resolu))
End If
Next i
End If
Return MyBase.Problems
End Function
'检查命名规范
Private Function CheckField(ByVal strName As String, ByVal strAbbreviation As String) As Boolean
Dim intLength As Integer = strAbbreviation.Length
Try
If (strName.Substring(0, intLength) <> strAbbreviation) Then
Return True
End If
If Char.IsLower(strName.Chars(strAbbreviation.Length)) Then
Return True
End If
Catch obj1 As Exception
Return True
End Try
Return False
End Function
End Class
End Namespace
'创建人: 朱祁林'创建时间:2010-12-07
'修改人: 朱祁林'修改时间:2010-12-07
Imports Microsoft.FxCop.Sdk
Imports System
Namespace MySoftRules
Public Class FunctionParametersCheck
Inherits BaseIntrospectionRule
' Methods
Public Sub New()
MyBase.New("FunctionParametersCheck", "CustomRules.CustomXMLRules", GetType(FunctionParametersCheck).Assembly)
End Sub
Public Overrides Function Check(ByVal member As Member) As ProblemCollection
Dim method As Method = TryCast(member, Method)
If ((Not method Is Nothing) AndAlso (method.DeclaringMember Is Nothing)) Then
Dim i As Integer
For i = 0 To method.Parameters.Count - 1
'true:找到不符合规范的参数,false表示没有找到不符合规范的参数
Dim blnFoundAddString As Boolean = False
Dim strFullType As String = method.Parameters.Item(i).Type.FullName
Dim strType As String = strFullType.Substring((strFullType.LastIndexOf(".") + 1), ((strFullType.Length - strFullType.LastIndexOf(".")) - 1))
Dim strName As String = method.Parameters.Item(i).Name.Name
If strType.ToLower() = "string" Then
blnFoundAddString = CheckField(strName, "str")
ElseIf strType.ToLower() = "int32" Then
blnFoundAddString = CheckField(strName, "int")
End If
If blnFoundAddString Then
Dim resolu As Resolution = MyBase.GetResolution(method.ToString, strName)
MyBase.Problems.Add(New Problem(resolu))
End If
Next i
End If
Return MyBase.Problems
End Function
'检查命名规范
Private Function CheckField(ByVal strName As String, ByVal strAbbreviation As String) As Boolean
Dim intLength As Integer = strAbbreviation.Length
Try
If (strName.Substring(0, intLength) <> strAbbreviation) Then
Return True
End If
If Char.IsLower(strName.Chars(strAbbreviation.Length)) Then
Return True
End If
Catch obj1 As Exception
Return True
End Try
Return False
End Function
End Class
End Namespace
从上面代码可以看出,在类的构造函数中,关联了开始定义的XML文件。为了简便起见,这里只对string和int两种类型说明。
得到CustomRules.dll这个最终的输出。
使用:有两种方法使用。
1、使用fxcop工具,见前文
2、与vs2008集成,将这个dll放置到:VS2008安装路径\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\Rules\目录下。选择一个项目,鼠标右键点击“运行代码分析”。