[Mono-list] GetTypeCode for Enum

Nick Drochak ndrochak@gol.com
Mon, 11 Mar 2002 15:27:09 +0900


This is a multi-part message in MIME format.

------=_NextPart_000_0020_01C1C911.35754770
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

| The trick is based on the fact that under CLR class that implements
| interface must implement all methods of that interface, but not necessary
| under the same name.
| See .override directive description in Partition II (9.3.2) for details.
| C# syntax for this is to explicitly include interface name in declaration
| (and without any visibility modifiers):
|    bool IConvertible.ToBool(...) {...}
| This way method becomes "visible" when instance is casted to
| interface type.
|    ((IConvertible)MyEnum).ToBool(...);
|

Thanks Sergey.  That was clear.

I put in the IConvertible interface stubs using that technique (see
attachment). However, I'm not sure what they do in mscorlib since they are
not public, and the compiler won't even allow the cast from an enum value to
an IConvertible.  Code such as below does not compile:

enum TestEnum {eeny, miney, moe};
//...
TestEnum e = TestEnum.eeny;
bool b = ((IConvertible)e).ToBoolean();  // <- compile error

I suppose those methods only need to be implemented if and when the runtime
needs them.

Thanks again,
Nick D.

------=_NextPart_000_0020_01C1C911.35754770
Content-Type: application/octet-stream;
	name="enum.diff"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="enum.diff"

Index: Enum.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/class/corlib/System/Enum.cs,v=0A=
retrieving revision 1.14=0A=
diff -u -r1.14 Enum.cs=0A=
--- Enum.cs	2002/03/01 14:41:46	1.14=0A=
+++ Enum.cs	2002/03/11 02:19:00=0A=
@@ -28,7 +28,112 @@=0A=
 	};=0A=
 =0A=
 	[MonoTODO]=0A=
-	public abstract class Enum : ValueType, IComparable {=0A=
+	public abstract class Enum : ValueType, IComparable, IConvertible {=0A=
+=0A=
+		// IConvertible methods Start -->=0A=
+=0A=
+		[CLSCompliant(false)]=0A=
+		public TypeCode GetTypeCode () {=0A=
+			MonoEnumInfo info;=0A=
+			MonoEnumInfo.GetInfo (this.GetType (), out info);=0A=
+			return Type.GetTypeCode (info.utype);=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		bool IConvertible.ToBoolean (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		byte IConvertible.ToByte (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		char IConvertible.ToChar (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		DateTime IConvertible.ToDateTime (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		decimal IConvertible.ToDecimal (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		double IConvertible.ToDouble (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		short IConvertible.ToInt16 (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		int IConvertible.ToInt32 (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		long IConvertible.ToInt64 (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+    		[CLSCompliant(false)]=0A=
+		sbyte IConvertible.ToSByte (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		float IConvertible.ToSingle (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+		object IConvertible.ToType (Type conversionType, IFormatProvider =
provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+		=0A=
+		[MonoTODO]=0A=
+    		[CLSCompliant(false)]=0A=
+		public ushort ToUInt16 (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+    		[CLSCompliant(false)]=0A=
+		uint IConvertible.ToUInt32 (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		[MonoTODO]=0A=
+    		[CLSCompliant(false)]=0A=
+		ulong IConvertible.ToUInt64 (IFormatProvider provider)=0A=
+		{=0A=
+			throw new NotImplementedException ();=0A=
+		}=0A=
+=0A=
+		// <-- End IConvertible methods=0A=
 =0A=
 		[MethodImplAttribute(MethodImplOptions.InternalCall)]=0A=
 		private extern object get_value ();=0A=

------=_NextPart_000_0020_01C1C911.35754770--