Code generation for property 'XXX' failed. Error was: .....

H

Hazim

I am designing a component to be used on windows applications.

'code''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class SomeComponent
Inherits System.ComponentModel.Component

Private _gaugeSettings As New GaugeConfiguration
Public Property GaugeSettings() As GaugeConfiguration
....
end property

End Class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I want user to be able to configure the GaugeSettings within the
Development environment.

'code''''''''''''''''''''''''''''''''''''''''''''''''''''''''
<TypeConverter(GetType(GaugeConfigurationConvertor)), _
DescriptionAttribute("Expand to see the GaugeSettings.")> _
Public Class GaugeConfiguration
......
End Class


Public Class GaugeConfigurationConvertor
Inherits ExpandableObjectConverter

Public Overloads Overrides Function CanConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As
System.Type) As Boolean
If destinationType Is GetType(GaugeConfiguration) Then
Return True

End If

Return MyBase.CanConvertFrom(context, destinationType)

End Function

Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
If (destinationType Is GetType(System.String) AndAlso TypeOf
value Is GaugeConfiguration) Then
Dim gaugeSettings As GaugeConfiguration = CType(value,
GaugeConfiguration)

Return "string representation of the class"

End If
Return MyBase.ConvertTo(context, culture, value, destinationType)

End Function

Public Overloads Overrides Function ConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object) As Object
If (TypeOf value Is String) Then
Try
Dim gaugeSettings As New GaugeConfiguration
.....
Return gaugeSettings

Catch
Throw New ArgumentException("Can not convert '" &
CStr(value) & "' to GaugeConfiguration")
End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
End Class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


When I try to configure this SomeComponent object on a form, things work
fine until I try to save the form, when I try to save, I get the
following error;

Code generation for property 'GaugeSettings' failed. Error was:
''GaugeConfigurationConvertor' is unable to convert
'LaserGaugeControl.GaugeConfiguration' to
'System.ComponentModel.Design.Serialization.InstanceDescriptor'.'

I actually added Serialization support to GaugeSettings class and a
clone method, still I kept getting the same error.
Can anyone help?

Thanks a lot,
Hazim...
 
C

Claes Bergefall

Your CanConvertTo and ConvertTo must handle the case when
destinationType is InstanceDescriptor.It tells the designer which
constructor to use. Basically it looks like this:

Imports System.ComponentModel

Public Overloads Overrides Function CanConvertTo(ByVal context As
ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
If
destinationType.Equals(GetType(Design.Serialization.InstanceDescriptor))
Then
Return True
Else
Return MyBase.CanConvertTo(context, destinationType)
End If
End Function

Public Overloads Overrides Function ConvertTo(ByVal context As
ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo,
ByVal value As Object, ByVal destinationType As System.Type) As Object
If Not
destinationType.Equals(GetType(Design.Serialization.InstanceDescriptor))
Then
Return MyBase.ConvertTo(context, culture, value, destinationType)
ElseIf Not TypeOf value Is GaugeConfiguration Then
Return MyBase.ConvertTo(context, culture, value, destinationType)
End If

Dim ctor As ConstructorInfo
ctor =
GetType(GaugeConfiguration).GetConstructor(System.Type.EmptyTypes)
Return New Design.Serialization.InstanceDescriptor(ctor, Nothing, False)
End Function

The above example uses a GaugeConfiguration constructor that has no
arguments.
There are other overloads of the GetConstructor method and the
InstanceDescriptor
constructor if your GaugeConfiguration constructor has arguments.

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top