Also, wenn das Property serialisiert wird (und das ist eine Designer Funktion, hat nichts mit <Serializable> usw zu tun) wird Code generiert in InitializeComponent().
Dein Property wird so initialisiert (mehr oder weniger)
'
'MyControl1
'
Me.MyControl1.Location = New System.Drawing.Point(72, 80)
Me.MyControl1.Class1ArrayProperty = New Class1() {new Class1( _
..werte..), new Class1(...werte...), ...}
'usw Nun muss der Designer wissen wie dieser Code zu generieren ist:
New Class1() {new Class1(..werte..), new Class1(...werte...), ...}
Und das sagt ihm ein TypeConverter.
Hier ist ein Beispiel von mir. Klasse:
<TypeConverter(GetType(VTPropertyTypeConverter))> _
Public Structure VTProperty
Private _f1 As Integer
Private _f2 As Integer
Public Sub New(ByVal f1 As Integer, ByVal f2 As Integer)
_f1 = f1
_f2 = f2
End Sub
<DefaultValue(0)> _
Public Property f1() As Integer
Get
Return _f1
End Get
Set(ByVal Value As Integer)
_f1 = Value
End Set
End Property
<DefaultValue(0)> _
Public Property f2() As Integer
Get
Return _f2
End Get
Set(ByVal Value As Integer)
_f2 = Value
End Set
End Property
Public Overrides Function ToString() As String
Return _f1.ToString() + ", " + _f2.ToString()
End Function
Public Shared Function Parse(ByVal sval As String) As VTProperty
Dim svals() As String = sval.Split(","c)
If svals.Length <> 2 Then
Throw New NotSupportedException("Invalid format")
End If
Try
Dim f1 As Integer = Integer.Parse(svals(0).Trim())
Dim f2 As Integer = Integer.Parse(svals(1).Trim())
Return New VTProperty(f1, f2)
Catch
Throw New NotSupportedException("Invalid format")
End Try
End Function
End Structure Type Converter:
Public Class VTPropertyTypeConverter
Inherits ExpandableObjectConverter
Public Sub New()
MyBase.New()
End Sub
Public Overloads Overrides Function CanConvertFrom(ByVal context As _
ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
If sourceType Is GetType(String) Then Return True
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal context As _
ITypeDescriptorContext, ByVal culture As _
System.Globalization.CultureInfo, ByVal value As Object) As Object
If value Is Nothing Then Return Nothing
Dim sval As String = value
If sval Is Nothing Then Throw New NotSupportedException("Unsuported" & _
"type")
Return VTProperty.Parse(sval)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As _
ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
If destinationType Is GetType(InstanceDescriptor) Then Return True
Return MyBase.CanConvertTo(context, destinationType)
End Function
Public Overloads Overrides Function ConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, _
ByVal destinationType As Type) As Object
If destinationType Is GetType(InstanceDescriptor) AndAlso value.GetType( _
) Is GetType(VTProperty) Then
Dim vtp As VTProperty = value
Dim ctor As ConstructorInfo = GetType(VTProperty).GetConstructor( _
New Type() {GetType(Integer), GetType(Integer)})
If Not ctor Is Nothing Then
Return New InstanceDescriptor(ctor, New Object() {vtp.f1, _
vtp.f2})
End If
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class |