Question

I have a really simple scenario where I just want to extend the functionality of the System.ComponentModel.BooleanConverter so that it will allow multiple options rather than just true and false.

So for example values like yes, 1, on etc. are the same as true.

I tried overriding the GetStandardValues() methods to do a quick test, my assumption being that if I returned a collection of all my specific values that a call to IsValid(string) should return true if I pass in one of my defined values but this does not appear to be the case. Do I have to implement/override a whole bunch of crap to do this?

I'm wondering if it's worth it since all I really need is the IsValid() functionality. Or perhaps there is a better option than using TypeDescriptors in the first place?

Was it helpful?

Solution

If you "only need IsValid functionality" then it should be enough to override just it. However, in practice CanConvertFrom(Type) is actually the method you should override if you're just looking to test if type can be converted. This method is used by some infrastructure classes, and just overriding IsValid might not be enough in that case.

For boolean converter you should also override ConvertFrom(object), and just return null if you're not going to use the value. Otherwise, calling IsValid (which in turn calls CanConvertFrom) could fail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top