📄 person.vb
字号:
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization
<System.ComponentModel.TypeConverter(GetType(PersonConverter))> _
<Serializable()> _
Public Class Person
Public Name As String
Public Height As Integer
Public Weight As Double
End Class
Public Class PersonConverter
Inherits TypeConverter
Public Overrides Function CanConvertFrom _
(ByVal context As ITypeDescriptorContext, _
ByVal sourceType As Type) As Boolean
If (sourceType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overrides Function CanConvertTo _
(ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean
If (destinationType Is GetType(InstanceDescriptor)) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function
Public Overrides Function ConvertFrom _
(ByVal context As ITypeDescriptorContext, _
ByVal culture As CultureInfo, _
ByVal value As Object) As Object
Dim text1 As String = TryCast(value, String)
If (text1 Is Nothing) Then
Return CreateInstance(Nothing) ' MyBase.ConvertFrom(context, culture, value)
End If
Dim text2 As String = text1.Trim
If (text2.Length = 0) Then
Return CreateInstance(Nothing)
End If
If (culture Is Nothing) Then
culture = CultureInfo.CurrentCulture
End If
Dim ch1 As Char = culture.TextInfo.ListSeparator.Chars(0)
Dim chArray1 As Char() = New Char() {ch1}
Dim textArray1 As String() = text2.Split(chArray1)
If (textArray1.Length = 3) Then
Dim o As New Person
o.Name = textArray1(0)
o.Height = CInt(textArray1(1))
o.Weight = CDbl(textArray1(2))
Return o
End If
Throw New ArgumentException("Unable to convert From")
End Function
Public Overrides Function GetCreateInstanceSupported _
(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetProperties _
(ByVal context As ITypeDescriptorContext, _
ByVal value As Object, _
ByVal attributes As Attribute()) As PropertyDescriptorCollection
Dim collection1 As PropertyDescriptorCollection = _
TypeDescriptor.GetProperties(GetType(Person), attributes)
Dim textArray1 As String() = New String() {"Name", "Height", "Weight"}
Return collection1.Sort(textArray1)
End Function
Public Overrides Function ConvertTo _
(ByVal context As ITypeDescriptorContext, _
ByVal culture As CultureInfo, _
ByVal value As Object, _
ByVal destinationType As Type) As Object
If (destinationType Is Nothing) Then
Throw New ArgumentNullException("Unable to convert To")
End If
If TypeOf value Is Person Then
If (destinationType Is GetType(String)) Then
Dim o As Person = CType(value, Person)
If (culture Is Nothing) Then
culture = CultureInfo.CurrentCulture
End If
Dim text1 As String = (culture.TextInfo.ListSeparator & " ")
Dim converter1 As TypeConverter = _
TypeDescriptor.GetConverter(GetType(Integer))
Dim converter2 As TypeConverter = _
TypeDescriptor.GetConverter(GetType(Double))
Dim textArray1 As String() = New String(2) {}
Dim num1 As Integer = 0
textArray1(num1) = o.Name
num1 += 1
textArray1(num1) = _
converter1.ConvertToString(context, culture, o.Height)
num1 += 1
textArray1(num1) = _
converter2.ConvertToString(context, culture, o.Weight)
num1 += 1
Return String.Join(text1, textArray1)
End If
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overrides Function CreateInstance _
(ByVal context As ITypeDescriptorContext, _
ByVal propertyValues As IDictionary) As Object
Dim o As New Person
If (propertyValues Is Nothing) Then
Return o
End If
Dim obj1 As Object = propertyValues.Item("Name")
Dim obj2 As Object = propertyValues.Item("Height")
Dim obj3 As Object = propertyValues.Item("Weight")
If ( _
((obj1 Is Nothing) OrElse _
(obj2 Is Nothing) OrElse _
(obj3 Is Nothing)) _
OrElse _
(Not TypeOf obj1 Is String OrElse _
Not TypeOf obj2 Is Integer OrElse _
Not TypeOf obj3 Is Double) _
) Then
Throw New ArgumentException("Unable to Create Instance")
End If
o.Name = CType(obj1, String)
o.Height = CType(obj2, Integer)
o.Weight = CType(obj3, Double)
Return o
End Function
Public Overrides Function GetPropertiesSupported _
(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -