Imports System
Imports System.Collections
Imports System.ComponentModel
Public Class PropertySpec
Private m_attributes As Attribute()
Private m_category As String
Private m_defaultValue As Object
Private m_description As String
Private editor As String
Private m_name As String
Private type As String
Private typeConverter As String
Public Sub New(ByVal name As String, ByVal type As String)
Me.New(name, type, Nothing, Nothing, Nothing)
End Sub
Public Sub New(ByVal name As String, ByVal type As Type)
Me.New(name, type.AssemblyQualifiedName, Nothing, Nothing, Nothing)
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal category As String)
Me.New(name, type, category, Nothing, Nothing)
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal category As String)
Me.New(name, type.AssemblyQualifiedName, category, Nothing, Nothing)
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal category As String, ByVal description As String)
Me.New(name, type, category, description, Nothing)
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal category As String, ByVal description As String)
Me.New(name, type.AssemblyQualifiedName, category, description, Nothing)
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal category As String, ByVal description As String, ByVal defaultValue As Object)
Me.m_name = name
Me.type = type
Me.m_category = category
Me.m_description = description
Me.m_defaultValue = defaultValue
Me.m_attributes = Nothing
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal category As String, ByVal description As String, ByVal defaultValue As Object)
Me.New(name, type.AssemblyQualifiedName, category, description, defaultValue)
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As String, _
ByVal typeConverter As String)
Me.New(name, type, category, description, defaultValue)
Me.editor = editor
Me.typeConverter = typeConverter
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As String, _
ByVal typeConverter As String)
Me.New(name, type.AssemblyQualifiedName, category, description, defaultValue, editor, _
typeConverter)
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As Type, _
ByVal typeConverter As String)
Me.New(name, type, category, description, defaultValue, editor.AssemblyQualifiedName, _
typeConverter)
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As Type, _
ByVal typeConverter As String)
Me.New(name, type.AssemblyQualifiedName, category, description, defaultValue, editor.AssemblyQualifiedName, _
typeConverter)
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As String, _
ByVal typeConverter As Type)
Me.New(name, type, category, description, defaultValue, editor, _
typeConverter.AssemblyQualifiedName)
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As String, _
ByVal typeConverter As Type)
Me.New(name, type.AssemblyQualifiedName, category, description, defaultValue, editor, _
typeConverter.AssemblyQualifiedName)
End Sub
Public Sub New(ByVal name As String, ByVal type As String, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As Type, _
ByVal typeConverter As Type)
Me.New(name, type, category, description, defaultValue, editor.AssemblyQualifiedName, _
typeConverter.AssemblyQualifiedName)
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal category As String, ByVal description As String, ByVal defaultValue As Object, ByVal editor As Type, _
ByVal typeConverter As Type)
Me.New(name, type.AssemblyQualifiedName, category, description, defaultValue, editor.AssemblyQualifiedName, _
typeConverter.AssemblyQualifiedName)
End Sub
Public Property Attributes() As Attribute()
Get
Return m_attributes
End Get
Set(ByVal value As Attribute())
m_attributes = value
End Set
End Property
Public Property Category() As String
Get
Return m_category
End Get
Set(ByVal value As String)
m_category = value
End Set
End Property
Public Property ConverterTypeName() As String
Get
Return typeConverter
End Get
Set(ByVal value As String)
typeConverter = value
End Set
End Property
Public Property DefaultValue() As Object
Get
Return m_defaultValue
End Get
Set(ByVal value As Object)
m_defaultValue = value
End Set
End Property
Public Property Description() As String
Get
Return m_description
End Get
Set(ByVal value As String)
m_description = value
End Set
End Property
Public Property EditorTypeName() As String
Get
Return editor
End Get
Set(ByVal value As String)
editor = value
End Set
End Property
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
Public Property TypeName() As String
Get
Return type
End Get
Set(ByVal value As String)
type = value
End Set
End Property
End Class
Public Class PropertySpecEventArgs
Inherits EventArgs
Private m_property As PropertySpec
Private val As Object
Public Sub New(ByVal [property] As PropertySpec, ByVal val As Object)
Me.m_property = [property]
Me.val = val
End Sub
Public ReadOnly Property [Property]() As PropertySpec
Get
Return m_property
End Get
End Property
Public Property Value() As Object
Get
Return val
End Get
Set(ByVal value As Object)
val = value
End Set
End Property
End Class
Public Delegate Sub PropertySpecEventHandler(ByVal sender As Object, ByVal e As PropertySpecEventArgs)
Public Class PropertyBag
Implements ICustomTypeDescriptor
#Region "PropertySpecCollection class definition"
<Serializable()> _
Public Class PropertySpecCollection
Private innerArray As ArrayList
Public Sub New()
innerArray = New ArrayList()
End Sub
Public ReadOnly Property Count() As Integer
Get
Return innerArray.Count
End Get
End Property
Public ReadOnly Property IsFixedSize() As Boolean
Get
Return False
End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean
Get
Return False
End Get
End Property
Public ReadOnly Property IsSynchronized() As Boolean
Get
Return False
End Get
End Property
Private ReadOnly Property SyncRoot() As Object
Get
Return Nothing
End Get
End Property
Public Function Add(ByVal value As PropertySpec) As Integer
Dim index As Integer = innerArray.Add(value)
Return index
End Function
Public Sub AddRange(ByVal array As PropertySpec())
innerArray.AddRange(array)
End Sub
Public Sub Clear()
innerArray.Clear()
End Sub
Public Function Contains(ByVal item As PropertySpec) As Boolean
Return innerArray.Contains(item)
End Function
Public Function Contains(ByVal name As String) As Boolean
For Each spec As PropertySpec In innerArray
If spec.Name = name Then
Return True
End If
Next
Return False
End Function
Public Sub CopyTo(ByVal array As PropertySpec())
innerArray.CopyTo(array)
End Sub
Public Sub CopyTo(ByVal array As PropertySpec(), ByVal index As Integer)
innerArray.CopyTo(array, index)
End Sub
Public Function GetEnumerator() As IEnumerator
Return innerArray.GetEnumerator()
End Function
Public Function IndexOf(ByVal value As PropertySpec) As Integer
Return innerArray.IndexOf(value)
End Function
Public Function IndexOf(ByVal name As String) As Integer
Dim i As Integer = 0
For Each spec As PropertySpec In innerArray
If spec.Name = name Then
Return i
End If
i += 1
Next
Return -1
End Function
Public Sub Insert(ByVal index As Integer, ByVal value As PropertySpec)
innerArray.Insert(index, value)
End Sub
Public Sub Remove(ByVal obj As PropertySpec)
innerArray.Remove(obj)
End Sub
Public Sub Remove(ByVal name As String)
Dim index As Integer = IndexOf(name)
RemoveAt(index)
End Sub
Public Sub RemoveAt(ByVal index As Integer)
innerArray.RemoveAt(index)
End Sub
Public Function ToArray() As PropertySpec()
Return DirectCast(innerArray.ToArray(GetType(PropertySpec)), PropertySpec())
End Function
End Class
#End Region
#Region "PropertySpecDescriptor class definition"
Private Class PropertySpecDescriptor
Inherits PropertyDescriptor
Private bag As PropertyBag
Private item As PropertySpec
Public Sub New(ByVal item As PropertySpec, ByVal bag As PropertyBag, ByVal name As String, ByVal attrs As Attribute())
MyBase.New(name, attrs)
Me.bag = bag
Me.item = item
End Sub
Public Overloads Overrides ReadOnly Property ComponentType() As Type
Get
Return item.[GetType]()
End Get
End Property
Public Overloads Overrides ReadOnly Property IsReadOnly() As Boolean
Get
Return (Attributes.Matches(ReadOnlyAttribute.Yes))
End Get
End Property
Public Overloads Overrides ReadOnly Property PropertyType() As Type
Get
Return Type.[GetType](item.TypeName)
End Get
End Property
Public Overloads Overrides Function CanResetValue(ByVal component As Object) As Boolean
If item.DefaultValue Is Nothing Then
Return False
Else
Return Not Me.GetValue(component).Equals(item.DefaultValue)
End If
End Function
Public Overloads Overrides Function GetValue(ByVal component As Object) As Object
Dim e As New PropertySpecEventArgs(item, Nothing)
bag.OnGetValue(e)
Return e.Value
End Function
Public Overloads Overrides Sub ResetValue(ByVal component As Object)
SetValue(component, item.DefaultValue)
End Sub
Public Overloads Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
Dim e As New PropertySpecEventArgs(item, value)
bag.OnSetValue(e)
End Sub
Public Overloads Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
Dim val As Object = Me.GetValue(component)
If item.DefaultValue Is Nothing AndAlso val Is Nothing Then
Return False
Else
Return Not val.Equals(item.DefaultValue)
End If
End Function
End Class
#End Region
Private m_defaultProperty As String
Private m_properties As PropertySpecCollection
Public Sub New()
m_defaultProperty = Nothing
m_properties = New PropertySpecCollection()
End Sub
Public Property DefaultProperty() As String
Get
Return m_defaultProperty
End Get
Set(ByVal value As String)
m_defaultProperty = value
End Set
End Property
Public ReadOnly Property Properties() As PropertySpecCollection
Get
Return m_properties
End Get
End Property
Public Event GetValue As PropertySpecEventHandler
Public Event SetValue As PropertySpecEventHandler
Protected Overridable Sub OnGetValue(ByVal e As PropertySpecEventArgs)
RaiseEvent GetValue(Me, e)
End Sub
Protected Overridable Sub OnSetValue(ByVal e As PropertySpecEventArgs)
RaiseEvent SetValue(Me, e)
End Sub
Private Function GetAttributes() As AttributeCollection Implements ICustomTypeDescriptor.GetAttributes
Return TypeDescriptor.GetAttributes(Me, True)
End Function
Private Function GetClassName() As String Implements ICustomTypeDescriptor.GetClassName
Return TypeDescriptor.GetClassName(Me, True)
End Function
Private Function GetComponentName() As String Implements ICustomTypeDescriptor.GetComponentName
Return TypeDescriptor.GetComponentName(Me, True)
End Function
Private Function GetConverter() As TypeConverter Implements ICustomTypeDescriptor.GetConverter
Return TypeDescriptor.GetConverter(Me, True)
End Function
Private Function GetDefaultEvent() As EventDescriptor Implements ICustomTypeDescriptor.GetDefaultEvent
Return TypeDescriptor.GetDefaultEvent(Me, True)
End Function
Private Function GetDefaultProperty() As PropertyDescriptor Implements ICustomTypeDescriptor.GetDefaultProperty
Dim propertySpec As PropertySpec = Nothing
If m_defaultProperty IsNot Nothing Then
Dim index As Integer = m_properties.IndexOf(m_defaultProperty)
' propertySpec = m_properties(index)
End If
If propertySpec IsNot Nothing Then
Return New PropertySpecDescriptor(propertySpec, Me, propertySpec.Name, Nothing)
Else
Return Nothing
End If
End Function
Private Function GetEditor(ByVal editorBaseType As Type) As Object Implements ICustomTypeDescriptor.GetEditor
Return TypeDescriptor.GetEditor(Me, editorBaseType, True)
End Function
Private Function GetEvents() As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
Return TypeDescriptor.GetEvents(Me, True)
End Function
Private Function GetEvents(ByVal attributes As Attribute()) As EventDescriptorCollection Implements ICustomTypeDescriptor.GetEvents
Return TypeDescriptor.GetEvents(Me, attributes, True)
End Function
Private Function GetProperties() As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
Return DirectCast(Me, ICustomTypeDescriptor).GetProperties(New Attribute(-1) {})
End Function
Private Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection Implements ICustomTypeDescriptor.GetProperties
Dim props As New ArrayList()
For Each [property] As PropertySpec In m_properties
Dim attrs As New ArrayList()
If [property].Category IsNot Nothing Then
attrs.Add(New CategoryAttribute([property].Category))
End If
If [property].Description IsNot Nothing Then
attrs.Add(New DescriptionAttribute([property].Description))
End If
If [property].ConverterTypeName IsNot Nothing Then
attrs.Add(New TypeConverterAttribute([property].ConverterTypeName))
End If
If [property].Attributes IsNot Nothing Then
attrs.AddRange([property].Attributes)
End If
Dim attrArray As Attribute() = DirectCast(attrs.ToArray(GetType(Attribute)), Attribute())
Dim pd As New PropertySpecDescriptor([property], Me, [property].Name, attrArray)
props.Add(pd)
Next
Dim propArray As PropertyDescriptor() = DirectCast(props.ToArray(GetType(PropertyDescriptor)), PropertyDescriptor())
Return New PropertyDescriptorCollection(propArray)
End Function
Private Function GetPropertyOwner(ByVal pd As PropertyDescriptor) As Object Implements ICustomTypeDescriptor.GetPropertyOwner
Return Me
End Function
End Class
Public Class PropertyTable
Inherits PropertyBag
Private propValues As Hashtable
Public Sub New()
propValues = New Hashtable()
End Sub
Default Public Property Item(ByVal key As String) As Object
Get
Return propValues(key)
End Get
Set(ByVal value As Object)
propValues(key) = value
End Set
End Property
Protected Overloads Overrides Sub OnGetValue(ByVal e As PropertySpecEventArgs)
e.Value = propValues(e.[Property].Name)
MyBase.OnGetValue(e)
End Sub
Protected Overloads Overrides Sub OnSetValue(ByVal e As PropertySpecEventArgs)
propValues(e.[Property].Name) = e.Value
MyBase.OnSetValue(e)
End Sub
End Class