Imports System.ComponentModel

Namespace Data

    ''' <summary>
    ''' An object representing a default property descriptor.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DefaultPropertyDescriptor
        Inherits PropertyDescriptor

        Public Sub New(ByVal componentType As Type, ByVal name As String, ByVal propertyType As Type, ByVal getValueMethod As Func(Of String, Object, Object))
            Me.New(componentType, name, propertyType, getValueMethod, Nothing)
        End Sub

        Public Sub New(ByVal componentType As Type, ByVal name As String, ByVal propertyType As Type, ByVal getValueMethod As Func(Of String, Object, Object), ByVal setValueMethod As Action(Of String, Object, Object))
            MyBase.New(name, Nothing)
            Me._componentType = componentType
            Me._propertyType = propertyType
            Me._getValueMethod = getValueMethod
            Me._setValueMethod = setValueMethod
        End Sub

        Private _componentType As Type
        Private _propertyType As Type
        Private _getValueMethod As Func(Of String, Object, Object)
        Private _setValueMethod As Action(Of String, Object, Object)

        Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
            Return False
        End Function

        Public Overrides ReadOnly Property ComponentType() As System.Type
            Get
                Return Me._componentType
            End Get
        End Property

        Public Overrides Function GetValue(ByVal component As Object) As Object
            If Me._getValueMethod IsNot Nothing Then _
                Return Me._getValueMethod(Me.Name, component)
            Return Nothing
        End Function

        Public Overrides ReadOnly Property IsReadOnly() As Boolean
            Get
                Return (Me._setValueMethod Is Nothing)
            End Get
        End Property

        Public Overrides ReadOnly Property PropertyType() As System.Type
            Get
                Return Me._propertyType
            End Get
        End Property

        Public Overrides Sub ResetValue(ByVal component As Object)
            ' Do nothing
        End Sub

        Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
            If Me._setValueMethod IsNot Nothing Then _
                Me._setValueMethod(Me.Name, component, value)
        End Sub

        Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
            Return True
        End Function

    End Class

End Namespace