0

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?

M4N
  • 94,805
  • 45
  • 217
  • 260
snappymcsnap
  • 2,050
  • 2
  • 29
  • 53

1 Answers1

2

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.

Nikola Radosavljević
  • 6,871
  • 32
  • 44
  • Thanks for the reply Nikola. After some testing I discovered that this is not a good way to solve my issue. Using IsValid to test string values like this is not a good design decision on my part. What happens behind the scenes is that .NET throws a System.FormatException if it fails the check so that's a lot of extra stack overhead when using this heavily. – snappymcsnap Mar 05 '13 at 13:55