我创建了以下自定义类来处理特定字符串(“YYY-MM-DD”)作为动态今天值的表示。有几个 ou Widening、Narrowing 和 ToString 函数可以帮助程序员进行代码内转换。
我的问题是当我反序列化时,使用 XmlSerializer,一个使用 MyDateTime 的类。我虽然 XmlSerializer 会使用Narrowing Operator CType(rightSide As String) As MyDateTime
, 作为一种对象初始化程序,但在我记得它使用反射之前。
如果我希望 MyDateTime 的 xml 反序列化类属性被初始化,我该怎么办Narrowing Operator CType(rightSide As String) As MyDateTime
?
Public Class MyDateTime
Const cstUniversalTodayValue As String = "YYYY-MM-DD"
Dim _internalStringRepresentation As String
Dim _internaValue As MyDateTime
Public ReadOnly Property DynamicToday As Boolean
Get
Return _internalStringRepresentation = cstUniversalTodayValue
End Get
End Property
Public Shared Widening Operator CType(rightSide As MyDateTime) As MyDateTime
Return rightSide._internaValue
End Operator
Public Shared Widening Operator CType(rightSide As MyDateTime) As String
Return rightSide._internalStringRepresentation
End Operator
Public Shared Narrowing Operator CType(rightSide As MyDateTime) As MyDateTime
Return New MyDateTime With {._internalStringRepresentation = rightSide.ToString("yyyy-MM-dd"), ._internaValue = rightSide}
End Operator
Public Shared Narrowing Operator CType(rightSide As String) As MyDateTime
Dim instance = New MyDateTime With {._internalStringRepresentation = rightSide}
If instance.DynamicToday Then
instance._internaValue = DateTime.Today
Else
instance._internaValue = Convert.ToDateTime(rightSide)
End If
Return instance
End Operator
Public Overrides Function ToString() As String
Return Me
End Function
Public Overloads Function ToString(format As String) As String
Return _internaValue.ToString(format)
End Function
End Class