本文介绍了查找函数名称的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨.
我只是想知道.
是否可以找出函数名称的长度?
例如,我有一个要求,即项目中的所有函数名称的长度均不得超过32个字符.我能.一旦知道函数名称,就使用strlen().
但是如何识别项目文件中的功能?
Hi.
I just wondered.
is it possible to find out the length of a function name?
for example i have a requirement that all function names in a project should not exceed 32 characters in length. i can. use strlen() once i know the name of the function.
but how to recognize a function in the project files?
推荐答案
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports VSLangProj2
Imports VSLangProj90
Imports System.Diagnostics
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.VCCodeModel
Imports System.Text
'*************************************************************************
'DESCRIPTION: Tests all of the functions in the current file for proper len.
'
Sub TestFunctionNameLength()
' Walk all of the elements for the file and test with the helper delegate.
WalkFileCodeElements(AddressOf TestFnNameLenCallBack_)
End Sub
'*************************************************************************
'DESCRIPTION: A worker function to test the length of a function name.
'
Sub TestFnNameLenCallBack_(ByRef elt As VCCodeElement)
' Only process if the input element is a function.
If (elt.Kind() = vsCMElement.vsCMElementFunction) Then
If (elt.Name.Length > 32) Then
'
' ADD SCOLDING OF THE DEVELOPER OF THIS FUNCTION HERE!
'
End If
End If
End Sub
'*************************************************************************
Public Delegate Sub CodeElementDelegateCallBack(ByRef elt As VCCodeElement)
'*************************************************************************
' DESCRIPTION: Walks all of the code elements in the specified collection
' and uses each element in a call to the callback delegate
' passed into the function.
'
Sub WalkElementCodeElements(ByVal elts As VCCodeElements, _
ByVal callback As CodeElementDelegateCallBack)
For Each elt As VCCodeElement In elts
callback(elt)
Next
End Sub
'*************************************************************************
Sub WalkFileCodeElements(ByVal callback As CodeElementDelegateCallBack)
' First try to get the code model for the current file.
Dim vcCm As VCCodeModel
Try
vcCm = DTE.ActiveDocument.ProjectItem.FileCodeModel
Catch ex As Exception
End Try
' That did not work. The file is probably not part of the project.
' Attempt to use the active projects code model.
If (vcCm Is Nothing) Then
Dim dte2 As EnvDTE80.DTE2 = DTE
Dim e As EnvDTE.Properties = dte2.Solution.Properties
Dim project As Project = dte2.Solution.Projects.Item(1)
vcCm = project.CodeModel
' Validate these results. If they failed, drop out.
If (vcCm Is Nothing) Then
Return
End If
End If
WalkElementCodeElements(vcCm.CodeElements, callback)
End Sub
这篇关于查找函数名称的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!