这不是线程安全的,但工作得非常好,我的目的:
使用方法:
Private _formattedFormulaText As New Lazy(Of IEnumerable(Of Label))(Function() New List(Of Label) From { _
FormulaLabels0, FormulaLabels1, lblBrownFormula, FormulaLabels3, lblGreenFormula, _
lblOrangeFormula, lblSRedFormula, FormulaLabels7, lblFormulaTotal})
缺点 - 所有触及变量的代码都必须添加一个访问器.value
。我的代码通常在同一个函数中有5个这样的懒惰集合和每个集合的1-2个触点。
缺点插图:
Dim clearText = Sub(c As Control) c.Text = String.Empty
_formattedFormulaText.ToList.ForEach(clearText)
变得
Dim clearText = Sub(c As Control) c.Text = String.Empty
_formattedFormulaText.Value.ToList.ForEach(clearText)
不与在.NET 4中使用的调试器显示良好实践兼容,但将是容易使用反射器
添加
请注意,一个属性可能已经被用来消除对touchpo中的.value的需要整型:
Private ReadOnly Property FormattedText As IEnumerable(Of Label)
Get
Return _formattedFormulaText.Value
End Get
End Property
配套类:
''' <summary>
''' translated from http://msdn.microsoft.com/en-us/vcsharp/bb870976.aspx
''' </summary>
Public Class Lazy(Of T)
Private _func As Func(Of T)
Private _result As T
Private _hasValue As Boolean
Public Sub New(ByVal func As Func(Of T))
_func = func
_hasValue = False
End Sub
Public ReadOnly Property Value As T
Get
If Me._hasValue = False Then
_result = _func()
_hasValue = True
End If
Return _result
End Get
End Property
End Class
基于MSDN的文章设计Lazy Computation in C# 也从反光援助对
assembly\NativeImages_v4.0.30319_32\mscorlib\246f1a5abb686b9dcdf22d3505b08cea\mscorlib.ni.dll