Using the following ExpandableObjectConverter-derived class leads to a most
strange issue. The ConvertTo method receives an 'Object value' argument that
is supposed to be of my own Style class. That Style class is used as the
type of several properties in my custom control.
Now that works fine while there is only my control on a form. But after
another control of any kind is added, some strange things begin to happen -
from time to time the 'Object value' arguments passed to the convertor
cannot be casted any longer to the Style class - neither using
'(Style)value' nor 'value as Style' constructs. The funny thing is that the
watch in VS debugger really shows the value object as a Style instance with
all its members having appropriate values.
Can anyone please explain why ConvertTo is called with a value that cannot
be casted to my Style class, and still the debugger shows that value as a
perfect Style instance in the watch window? Thanks in advance, any logical
explanation will hopefully save me from utter madness.
internal class StyleConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(
ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(
ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
object value, Type destinationType)
{
Style style = value as Style;
if (destinationType == typeof(InstanceDescriptor) && style != null)
{
ConstructorInfo info = typeof(Style).GetConstructor(
new Type[] { typeof(string) });
if (info != null)
return new InstanceDescriptor(info, new object[] { style.Store() });
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
Mick Doherty - 23 May 2005 11:43 GMT
Is this a nested class? Some strange things like this happen when you nest
classes, structs or enums. If so, try moving it outside of any class, but
within a namespace.

Signature
Mick Doherty
http://dotnetrix.co.uk/nothing.html
> Using the following ExpandableObjectConverter-derived class leads to a
> most strange issue. The ConvertTo method receives an 'Object value'
[quoted text clipped - 43 lines]
> }
> }
D.Z. Simpson - 23 May 2005 11:51 GMT
To be more precise, when there is a second control on the form the code
generation always passes that strange value to ConvertTo.
Style is a top level class, does not inherit anything and only implements
Cloneable. It has some fonts and colors as members.
> Is this a nested class? Some strange things like this happen when you nest
> classes, structs or enums. If so, try moving it outside of any class, but
> within a namespace.
Frank Hileman - 24 May 2005 13:31 GMT
In this situation you probably have two copies of your dll loaded. Both have
a Style class, but they are not considered the same class by the designer
(one loaded via LoadFrom, one via Load). Are you using reflection at
design-time on this dll? If so, eliminate that, as it will cause problems.
If not, try strong naming your dll and installing in the GAC. This will help
prevent it from being loaded twice. Also search for old copies of your dll
being referenced, possibly from a bin folder.
Regards,
Frank Hileman
check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor
> Using the following ExpandableObjectConverter-derived class leads to a
> most strange issue. The ConvertTo method receives an 'Object value'
[quoted text clipped - 43 lines]
> }
> }
D.Z. Simpson - 30 May 2005 08:03 GMT
Thank you very much. Indeed it seems VS loads a second copy of the dll
that's referenced from the toolbox when I add additional controls to the
form. Otherwise it uses just one copy from the reference path of the project
and code generation works fine.
Now I have strong-named the assembly and everything works great, even
without installing to the GAC.
Thanks again.
> In this situation you probably have two copies of your dll loaded. Both
> have a Style class, but they are not considered the same class by the
[quoted text clipped - 60 lines]
>> }
>> }