L
Larry Smith
Hi there,
Can someone confirm the correct use of a "TypeConverter.CanConvertTo()" override as it applies to string conversions. The canonical example looks like this:
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
Two confusing issues exist however:
1) This never actually gets called. Based on what I'ver read, I think string conversions are always permitted so the system has no reason to call it (and therefore never does). Can someone confirm this and if so, why do so many examples on the web (including MSFT's) code things this way if it's never actually called.
2) I've seen other examples from senior MSFT staff where they don't actually test for "typeof(string)" as seen above. Instead, they test for the class this "TypeConverter" derivative was created for. That seems to make little sense (testing if a class can be converted to itself) and moreover, in the corresponding "ConvertTo()" override, they convert it to a string anyway. Can anyone explain this. Thanks.
Can someone confirm the correct use of a "TypeConverter.CanConvertTo()" override as it applies to string conversions. The canonical example looks like this:
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
Two confusing issues exist however:
1) This never actually gets called. Based on what I'ver read, I think string conversions are always permitted so the system has no reason to call it (and therefore never does). Can someone confirm this and if so, why do so many examples on the web (including MSFT's) code things this way if it's never actually called.
2) I've seen other examples from senior MSFT staff where they don't actually test for "typeof(string)" as seen above. Instead, they test for the class this "TypeConverter" derivative was created for. That seems to make little sense (testing if a class can be converted to itself) and moreover, in the corresponding "ConvertTo()" override, they convert it to a string anyway. Can anyone explain this. Thanks.