[Mono-list] Re: IConvertible and Enum

Derek Holden dsh2120@draper.com
Mon, 14 Jan 2002 08:01:15 -0500


I brought this up when I was doing System.Convert. Basically should 
System.Convert do all of the conversions and have the base types use it 
when implementing IConvertible, or should the base types define their own 
conversions through IConvertible have and System.Convert just be a wrapper 
for those methods. Nothing was decided so I ended up taking out the 
IConvertible methods and commenting it out of the class declarations.

The only thing that is funky about doing it the first way is in the 
undefined explicit conversions. For instance, there is no conversion from 
Single to Char in the EMCA world. However, you'll need to have a 
Single.ToChar() when implementing IConvertible, and Single needs to throw 
an exception. But, calling Single.ToInt32() with an out of range value will 
rely on System.Convert to throw the exception. I don't know if that's 
questionable enough to impact anything, but it's worth noting.

Certain behavior leads me to believe MS did it the 2nd way. I think they 
implemented IConvertible in the core data types, then wrote System.Convert 
as a wrapper to all of those. There is no System.Convert.ToChar(Single) in 
the EMCA world, but it exists in .NET's System.Convert, albeit throwing an 

In short, I don't care either way. I say uncomment the IConvertible 
declaration in the base types and have the methods just call the 
System.Convert methods. The only thing I ask is you take a look at the EMCA 
conversion chart and only allow those explicit conversions. Oh, and there's 
also a helper function defined in System.Convert for the ToType method. So 
in implementing 'object ToType (Type convesionType, IFormatProvider 
provider)' in the base types, you can just return Convert.ToType (value, 
conversionType, provider).

At 1/13/2002 06:12 PM, Andrei Zmievski wrote:
>Paolo mentioned that he needed Enum to support IConvertible for the 
>compiler, so I decided to take that on as my first contribution. Of 
>course, there is some ambiguity mention on the mailing list as to what 
>approach the IConvertible implementation should take. I see that Derek 
>implemented all the conversions in System.Convert instead of the base 
>types, and that  the base types currently do not implement IConvertible 
>interface. Is this subject to change? For System.Enum should I just 
>implement IConvertible and use System.Convert for the actual conversions?