Hi,
I get StackOverflow when I return an InstanceDescriptor which represents a
constructor which takes it's own type as parameter. Could someone explain
why return new InstanceDescriptor(ci,new object[]{date}); causes
StackOverflow? Is it again going into the ConvertTo? Why?
Short code snippet:
if(destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(ExtendedDate).GetConstructor(new
Type[]{typeof(ExtendedDate)});
ExtendedDate date = (ExtendedDate) value;
return new InstanceDescriptor(ci,new object[]{date});
}
Full sample code:
public class Component1 : System.ComponentModel.Component
{
public Component1(System.ComponentModel.IContainer container)
{
container.Add(this);
}
public Component1(){}
private ExtendedDate _date;
public ExtendedDate ExtendedDate
{
get { return _date; }
set { _date = value; }
}
bool ShouldSerializeExtendedDate()
{
return true;
}
}
[TypeConverter(typeof(ExtendedDateTypeConverter))]
public class ExtendedDate
{
public ExtendedDate(ExtendedDate date)
{
}
}
internal sealed class ExtendedDateTypeConverter: TypeConverter
{
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)
{
if(value is ExtendedDate)
{
if(destinationType == typeof(string))
return ((ExtendedDate)value).ToString();
else if(destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(ExtendedDate).GetConstructor(new
Type[]{typeof(ExtendedDate)});
ExtendedDate date = (ExtendedDate) value;
return new InstanceDescriptor(ci,new object[]{date});
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
if(sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom (context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if(value is string)
{
return new ExtendedDate(null);
}
return base.ConvertFrom (context, culture, value);
}
}