[Mono-bugs] [Bug 29811][Wis] New - Enum.Format doesn't always for for "x" format specifier

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
3 Sep 2002 15:25:44 -0000


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by jonpryor@vt.edu.

http://bugzilla.ximian.com/show_bug.cgi?id=29811

--- shadow/29811	Tue Sep  3 11:25:44 2002
+++ shadow/29811.tmp.4604	Tue Sep  3 11:25:44 2002
@@ -0,0 +1,137 @@
+Bug#: 29811
+Product: Mono/Class Libraries
+Version: unspecified
+OS: Solaris 7
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: jonpryor@vt.edu               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Enum.Format doesn't always for for "x" format specifier
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+Description of Problem:
+
+The "x" format specifier may generate an exception in Enum.Format for some
+enumeration values.
+
+Steps to reproduce the problem:
+1. Compile the following code, ``av.cs''.  The code is straightforward,
+printing out the "description" of an enumeration value, in which the
+description is the textual description of the enumeration value ("f" format
+specifier) followed by it's hex value (the "x" format specifier).  It does
+this for several different enumerations, all of which generate exceptions
+when formatted with the "x" specifier.
+
+        // file: av.cs
+        // attributes values
+
+        using System;
+        using System.Reflection;
+        using System.Text;
+
+        public class av {
+          
+          private static string EnumDescription (Type enumType, object value)
+          {
+            StringBuilder sb = new StringBuilder ();
+            sb.Append (String.Format("{0}: ", enumType.Name));
+            sb.Append (Enum.Format (enumType, value, "f"));
+            sb.Append (" (0x");
+            sb.Append (Enum.Format (enumType, value, "x"));
+            sb.Append (")");
+            return sb.ToString();
+          }
+
+          private static void PrintValue (object o)
+          {
+            try {
+              Console.WriteLine (EnumDescription (o.GetType(), o));
+            }
+            catch (Exception e) {
+              Console.WriteLine ("** exception with type: " + o.GetType());
+              Console.WriteLine ("   Message: " + e.Message);
+            }
+          }
+
+          public static void Main () {
+            EventAttributes ea = EventAttributes.SpecialName;
+            FieldAttributes fa = FieldAttributes.SpecialName;
+            MethodAttributes ma = MethodAttributes.SpecialName;
+            MethodImplAttributes mia = MethodImplAttributes.InternalCall;
+            ParameterAttributes pa = 
+              ParameterAttributes.In | ParameterAttributes.HasDefault;
+            PropertyAttributes pra = 
+              PropertyAttributes.SpecialName | PropertyAttributes.HasDefault;
+            ResourceAttributes ra = 
+              ResourceAttributes.Public | ResourceAttributes.Private;
+            TypeAttributes ta = TypeAttributes.SpecialName |
+TypeAttributes.Import;
+
+            object[] objs = {ea, fa, ma, mia, pa, pra, ra, ta};
+
+            Console.WriteLine ("objects:");
+
+            foreach (object o in objs) {
+              PrintValue (o);
+            }
+          }
+        }
+
+2. Run the code
+
+Actual Results:
+objects:
+** exception with type: System.Reflection.EventAttributes
+   Message: Invalid format
+** exception with type: System.Reflection.FieldAttributes
+   Message: Invalid format
+** exception with type: System.Reflection.MethodAttributes
+   Message: Invalid format
+** exception with type: System.Reflection.MethodImplAttributes
+   Message: Invalid format
+** exception with type: System.Reflection.ParameterAttributes
+   Message: Invalid format
+** exception with type: System.Reflection.PropertyAttributes
+   Message: Invalid format
+** exception with type: System.Reflection.ResourceAttributes
+   Message: Invalid format
+** exception with type: System.Reflection.TypeAttributes
+   Message: Invalid format
+
+
+
+Expected Results:
+
+objects:
+EventAttributes: SpecialName (0x200)
+FieldAttributes: SpecialName (0x200)
+MethodAttributes: SpecialName (0x800)
+MethodImplAttributes: InternalCall (0x1000)
+ParameterAttributes: In, HasDefault (0x1001)
+PropertyAttributes: SpecialName, HasDefault (0x1200)
+ResourceAttributes: Public, Private (0x3)
+TypeAttributes: SpecialName, Import (0x1400)
+
+How often does this happen? 
+
+Always.
+
+Additional Information:
+
+The problem is due to Enum.cs:443.  The code assumes that
+`<enum-value>.ToString()' will return a number which can be parsed with
+Int64.Parse.  This is not the case -- ToString() will often return the
+textual form of the enumeration value, e.g. EventAttributes.SpecialName
+instead of 512.  Int64.Parse thus throws an exception, preventing the
+actual enumeration value from being returned.
+
+I have a patch pending, which I'll submit to [mono-list] shortly.