我有一个有100+-公共静态方法的类,我想每次都限制一个静态调用,有没有什么技巧可以用几行代码来实现这一点? 像一些类属性来实现这一点?
像这样:
public class Test
{
public static void A() { }
public static void B() { }
public static void C() { }
public static void D() { }
}
一次只能访问一个静态方法调用。方法B调用时,必须阻塞其他方法的调用,直到方法B返回;
对不起,我似乎发布了一些误导性的标题。我的真正目的是找到一种减少编码的方法,以通过在每个函数中添加lock语句来达到相同的结果。
解决了
我最后通过向每个函数添加锁定语句解决了这个问题,但是使用下面的宏来为我做重复的工作。
''
'' edit from Code outlining sample.
''
Sub LoopFunction()
Dim i As Integer
Dim fileCM As FileCodeModel
Dim elts As EnvDTE.CodeElements
Dim elt As EnvDTE.CodeElement
fileCM = DTE.ActiveDocument.ProjectItem.FileCodeModel
elts = fileCM.CodeElements
For i = 1 To elts.Count
elt = elts.Item(i)
EditFunction(elt)
Next
End Sub
Sub EditFunction(ByVal elt As EnvDTE.CodeElement)
If (elt.Kind = EnvDTE.vsCMElement.vsCMElementNamespace) Then
Dim i As Integer
Dim mems As EnvDTE.CodeElements
mems = elt.Members
For i = 1 To mems.Count
EditFunction(mems.Item(i))
Next
ElseIf (elt.Kind = vsCMElement.vsCMElementClass) Then
Dim i As Integer
Dim mems As EnvDTE.CodeElements
mems = elt.Members
For i = 1 To mems.Count
EditFunction(mems.Item(i))
Next
ElseIf (elt.Kind = vsCMElement.vsCMElementFunction) Then
Dim epStart As EnvDTE.EditPoint
Dim epEnd As EnvDTE.EditPoint
Dim textSelection As EnvDTE.TextSelection
epStart = elt.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint()
epEnd = elt.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint()
textSelection = DTE.ActiveDocument.Selection
textSelection.MoveToPoint(epEnd)
textSelection.Insert(NewLine & "}" & NewLine)
textSelection.MoveToPoint(epStart)
textSelection.Insert(NewLine & "lock(lockTest){" & NewLine)
End If
End Sub