[Mono-bugs] [Bug 40123][Min] New - Type.IsInstanceOfType does not handle Interfaces.

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Fri, 21 Mar 2003 13:57:17 -0500 (EST)


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 tom@acquist.com.

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

--- shadow/40123	Fri Mar 21 13:57:17 2003
+++ shadow/40123.tmp.5437	Fri Mar 21 13:57:17 2003
@@ -0,0 +1,101 @@
+Bug#: 40123
+Product: Mono/Class Libraries
+Version: unspecified
+OS: All
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Minor
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: tom@acquist.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Type.IsInstanceOfType does not handle Interfaces.
+
+Description of Problem:
+The Type.IsInstanceOfType method does not return true if the type is an 
+interface and the object is a member of a class that implements that 
+interface.
+
+
+Steps to reproduce the problem:
+Compile and run the following code.
+
+using System;
+
+public class TypeError
+{
+  public static void Main()
+  {
+    int x = 0;
+    int y = 15;
+    Console.WriteLine("----Should all be true----");
+    Console.WriteLine(x.GetType().IsInstanceOfType(x));
+    Console.WriteLine(x.GetType().IsInstanceOfType(y));
+    Console.WriteLine(typeof(int).IsInstanceOfType(x));
+    Console.WriteLine(typeof(ValueType).IsInstanceOfType(x));
+    Console.WriteLine(typeof(object).IsInstanceOfType(x));
+    Console.WriteLine(typeof(IComparable).IsInstanceOfType(x));
+    Console.WriteLine(typeof(IFormattable).IsInstanceOfType(x));
+    Console.WriteLine(typeof(IConvertible).IsInstanceOfType(x));
+  }
+}
+
+
+Actual Results:
+IsInstanceOfType will return false for the three interfaces, even though 
+int implements those interfaces.  The output of the example program is:
+
+----Should all be true----
+True
+True
+True
+True
+True
+False
+False
+False
+
+
+Expected Results:
+
+IsInstanceOfType should return true for when the type is an interface and 
+the object is a class that implements that interface.  The output of he 
+example program run on .Net 1.1 is:
+
+----Should all be true----
+True
+True
+True
+True
+True
+True
+True
+True
+
+How often does this happen? 
+Every time.
+
+Additional Information:
+
+A potential fix for IsInstanceOfType is as follows:
+
+      public bool IsInstanceOfType (object o) {
+         if (o != null) {
+            Type t = o.GetType ();
+            if((t == this) || t.IsSubclassOf (this))
+            {
+               return true;
+            }
+            if(this.IsInterface)
+            {
+               Type [] ifaces = t.GetInterfaces ();
+               return (Array.IndexOf (ifaces, this) != -1);
+            }
+         }
+         return false;
+      }