[Mono-list] [PATCH] addition of some non-CLS-compliant methods to System.Convert

Kristian Rietveld kris@gtk.org
03 Jan 2002 00:19:16 +0100


Hey,

While trying to get NAnt to work, I found out that NAnt needs the 

public static bool ToBoolean (object value)

method in System.Convert, which isn't there. So I cooked up the
following patch (with help of John Barnette) which adds this method and
two others, which aren't non-CLS-compliant, but are being mentioned in
the SDK documentation.

WARNING: This is the first C# code I ever wrote (other than the
obligatory Hello World) and is untested.

Anyway, here's the ChangeLog, patch has been appended.

2002-01-03  Kristian Rietveld  <kris@gtk.org>

        * Convert.cs: add non-CLS-compliant ToBoolean methods for char,
        DateTime and object.


regards,


	Kris


Index: Convert.cs
===================================================================
RCS file: /mono/mcs/class/corlib/System/Convert.cs,v
retrieving revision 1.5
diff -u -r1.5 Convert.cs
--- Convert.cs	21 Nov 2001 18:12:11 -0000	1.5
+++ Convert.cs	2 Jan 2002 23:00:28 -0000
@@ -164,6 +164,16 @@
 			return (value != 0); 
 		}
 
+		public static bool ToBoolean (char value)
+		{
+			throw new InvalidCastException (Locale.GetText ("Can't convert char to bool"));
+		}
+
+		public static bool ToBoolean (DateTime value)
+		{
+			throw new InvalidCastException (Locale.GetText ("Can't convert date to bool"));
+		}
+
 		public static bool ToBoolean (decimal value) 
 		{ 
 			return (value != 0M); 
@@ -187,6 +197,14 @@
 		public static bool ToBoolean (long value) 
 		{ 
 			return (value != 0); 
+		}
+
+		public static bool ToBoolean (object value)
+		{
+			if (value == null || !(value is IConvertible))
+				return false;
+
+			return ((IConvertible)value).ToBoolean (CultureInfo.CurrentCulture);
 		}
 
 		public static bool ToBoolean (sbyte value)