⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 personconverter.vb

📁 讲解visual studio的应用
💻 VB
字号:
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization


''' <summary>
''' TypeConverter that enables a Person to be serialized
''' as a String for use with the Resource designer.
''' </summary>
''' <remarks></remarks>
Public Class PersonConverter
    Inherits TypeConverter

    ''' <summary>
    ''' Indicates if it is possible to convert from
    ''' a particular sourceType to a Person
    ''' </summary>
    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

    ''' <summary>
    ''' Used to convert from String to Person
    ''' </summary>
    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

    ''' <summary>
    ''' Indicates whether it is possible to convert to 
    ''' a Person object
    ''' </summary>
    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


    ''' <summary>
    ''' Converts to a Person object (from a String)
    ''' </summary>
    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


    ''' <summary>
    ''' Indicates whether a new Person can be created (true!)
    ''' </summary>
    Public Overrides Function GetCreateInstanceSupported _
             (ByVal context As ITypeDescriptorContext) As Boolean
        Return True
    End Function

    ''' <summary>
    ''' Creates a Person from a list of property values
    ''' </summary>
    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

    ''' <summary>
    ''' Indicates whether GetProperties can be called (true!)
    ''' </summary>
    Public Overrides Function GetPropertiesSupported _
             (ByVal context As ITypeDescriptorContext) As Boolean
        Return True
    End Function

    ''' <summary>
    ''' Returns a collection of property descriptors
    ''' </summary>
    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


End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -