Vb.net 遵守添加的列表
我有一张(定制类)的清单。 在我的代码中的某个地方,项目被添加到此列表中,尽管它们不应该被添加。 我的代码非常庞大,因此我无法轻松调试发生这种情况的地方。 因此,我想创建一个扩展,当我不希望插入/添加时,我可以轻松地插入它 有人能告诉我这是否可能,如果可能,怎么可能 我希望我能以某种方式检测出调用方(函数)在哪里进行添加Vb.net 遵守添加的列表,vb.net,list,Vb.net,List,我有一张(定制类)的清单。 在我的代码中的某个地方,项目被添加到此列表中,尽管它们不应该被添加。 我的代码非常庞大,因此我无法轻松调试发生这种情况的地方。 因此,我想创建一个扩展,当我不希望插入/添加时,我可以轻松地插入它 有人能告诉我这是否可能,如果可能,怎么可能 我希望我能以某种方式检测出调用方(函数)在哪里进行添加 谢谢。我想我已经有了: Imports System.Collections.ObjectModel Public Class clsCellListExtender
谢谢。我想我已经有了:
Imports System.Collections.ObjectModel
Public Class clsCellListExtender
Public Class List(Of T)
Inherits Collection(Of T)
Private _iID As Integer = 0
Protected Overrides Sub InsertItem(index As Integer, item As T)
'your checks here
If TypeOf (item) Is clsCell Then
_iID += 1
Dim nCell As clsCell = TryCast(item, clsCell)
nCell.TempID = _iID
End If
MyBase.InsertItem(index, item)
End Sub
End Class
End Class
我将我的列表声明为CLSCellListXtender.list(属于clsCell)。我想我已经有了它:
Imports System.Collections.ObjectModel
Public Class clsCellListExtender
Public Class List(Of T)
Inherits Collection(Of T)
Private _iID As Integer = 0
Protected Overrides Sub InsertItem(index As Integer, item As T)
'your checks here
If TypeOf (item) Is clsCell Then
_iID += 1
Dim nCell As clsCell = TryCast(item, clsCell)
nCell.TempID = _iID
End If
MyBase.InsertItem(index, item)
End Sub
End Class
End Class
我将我的列表声明为CLSCellListXtender.list(属于clsCell)。编写集合类而不是使用集合对象的好处之一是,您可以在
Add
方法中设置断点来捕获这些内容。使用BindingList(属于T)AddNew事件或ObservableCollection(属于T)您可以始终使用decorator模式并实现自己的日志decorator,该decorator实现您包装的List(Of T)
。然后用新类替换所有出现的List(of T)
,您可以记录每个列表操作。编写集合类而不是使用集合对象的好处之一是,您可以在Add
方法中设置断点来捕获这些内容。使用BindingList(of T)AddNew事件或observeCollection(of T)您可以始终使用decorator模式并实现自己的日志decorator,该decorator实现您包装的List(Of T)
。然后用新类替换所有出现的List(of T)
,您可以记录每个List操作。