Author: gert
Date: 2008-02-12 14:49:57 -0500 (Tue, 12 Feb 2008)
New Revision: 95549

Modified:
   trunk/mcs/class/corlib/System/ChangeLog
   trunk/mcs/class/corlib/System/Delegate.cs
   trunk/mcs/class/corlib/Test/System/ChangeLog
   trunk/mcs/class/corlib/Test/System/DelegateTest.cs
Log:
* DelegateTest.cs: Added more tests for CreateDelegate, dealing with
a non-matching return type, case, arguments and inherited methods.
* Delegate.cs (CreateDelegate): Walk the inheritance change to find
matching method. Skip methods for which the return type does not match
the delegate. Fixes bug #360784.


Modified: trunk/mcs/class/corlib/System/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System/ChangeLog     2008-02-12 19:35:19 UTC (rev 
95548)
+++ trunk/mcs/class/corlib/System/ChangeLog     2008-02-12 19:49:57 UTC (rev 
95549)
@@ -1,3 +1,9 @@
+2008-02-12  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * Delegate.cs (CreateDelegate): Walk the inheritance change to find
+       matching method. Skip methods for which the return type does not match
+       the delegate. Fixes bug #360784.
+
 2008-02-11  Eyal Alaluf <[EMAIL PROTECTED]>
 
        * NumberFormatter.cs: Fix ToString("R") for +-Infinity & NaN.

Modified: trunk/mcs/class/corlib/System/Delegate.cs
===================================================================
--- trunk/mcs/class/corlib/System/Delegate.cs   2008-02-12 19:35:19 UTC (rev 
95548)
+++ trunk/mcs/class/corlib/System/Delegate.cs   2008-02-12 19:49:57 UTC (rev 
95549)
@@ -296,7 +296,8 @@
                        if (!type.IsSubclassOf (typeof (MulticastDelegate)))
                                throw new ArgumentException ("type is not 
subclass of MulticastDelegate.");
 
-                       ParameterInfo[] delargs = type.GetMethod 
("Invoke").GetParameters ();
+                       MethodInfo invoke = type.GetMethod ("Invoke");
+                       ParameterInfo [] delargs = invoke.GetParameters ();
                        Type[] delargtypes = new Type [delargs.Length];
 
                        for (int i=0; i<delargs.Length; i++)
@@ -306,11 +307,29 @@
                         * FIXME: we should check the caller has reflection 
permission
                         * or if it lives in the same assembly...
                         */
-                       BindingFlags flags = BindingFlags.ExactBinding | 
BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;
+
+                       /*
+                        * since we need to walk the inheritance chain anyway to
+                        * find private methods, adjust the bindingflags to 
ignore
+                        * inherited methods
+                        */
+                       BindingFlags flags = BindingFlags.ExactBinding |
+                               BindingFlags.Public | BindingFlags.Static |
+                               BindingFlags.NonPublic | 
BindingFlags.DeclaredOnly;
                        if (ignoreCase)
                                flags |= BindingFlags.IgnoreCase;
-                       MethodInfo info = target.GetMethod (method, flags, 
null, delargtypes, new ParameterModifier [0]);
 
+                       MethodInfo info = null;
+
+                       for (Type targetType = target; targetType != null; 
targetType = targetType.BaseType) {
+                               MethodInfo mi = targetType.GetMethod (method, 
flags,
+                                       null, delargtypes, new 
ParameterModifier [0]);
+                               if (mi != null && return_type_match 
(invoke.ReturnType, mi.ReturnType)) {
+                                       info = mi;
+                                       break;
+                               }
+                       }
+
                        if (info == null) {
                                if (throwOnBindFailure)
                                        throw new ArgumentException ("Couldn't 
bind to method.");
@@ -350,7 +369,8 @@
                        if (!type.IsSubclassOf (typeof (MulticastDelegate)))
                                throw new ArgumentException ("type");
 
-                       ParameterInfo[] delargs = type.GetMethod 
("Invoke").GetParameters ();
+                       MethodInfo invoke = type.GetMethod ("Invoke");
+                       ParameterInfo[] delargs = invoke.GetParameters ();
                        Type[] delargtypes = new Type [delargs.Length];
 
                        for (int i=0; i<delargs.Length; i++)
@@ -360,13 +380,30 @@
                         * FIXME: we should check the caller has reflection 
permission
                         * or if it lives in the same assembly...
                         */
-                       BindingFlags flags = BindingFlags.ExactBinding | 
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
 
+                       /*
+                        * since we need to walk the inheritance chain anyway to
+                        * find private methods, adjust the bindingflags to 
ignore
+                        * inherited methods
+                        */
+                       BindingFlags flags = BindingFlags.ExactBinding | 
+                               BindingFlags.NonPublic | BindingFlags.Public |
+                               BindingFlags.Instance | 
BindingFlags.DeclaredOnly;
+
                        if (ignoreCase)
                                flags |= BindingFlags.IgnoreCase;
 
-                       MethodInfo info = target.GetType ().GetMethod (method, 
flags, null, delargtypes, new ParameterModifier [0]);
+                       MethodInfo info = null;
 
+                       for (Type targetType = target.GetType (); targetType != 
null; targetType = targetType.BaseType) {
+                               MethodInfo mi = targetType.GetMethod (method, 
flags,
+                                       null, delargtypes, new 
ParameterModifier [0]);
+                               if (mi != null && return_type_match 
(invoke.ReturnType, mi.ReturnType)) {
+                                       info = mi;
+                                       break;
+                               }
+                       }
+
                        if (info == null)
                                if (throwOnBindFailure)
                                        throw new ArgumentException ("Couldn't 
bind to method '" + method + "'.");

Modified: trunk/mcs/class/corlib/Test/System/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System/ChangeLog        2008-02-12 19:35:19 UTC 
(rev 95548)
+++ trunk/mcs/class/corlib/Test/System/ChangeLog        2008-02-12 19:49:57 UTC 
(rev 95549)
@@ -1,3 +1,8 @@
+2008-02-12  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * DelegateTest.cs: Added more tests for CreateDelegate, dealing with
+       a non-matching return type, case, arguments and inherited methods.
+
 2008-02-11  Gert Driesen  <[EMAIL PROTECTED]>
 
        * DelegateTest.cs: Added incomplete set of (notworking) tests for

Modified: trunk/mcs/class/corlib/Test/System/DelegateTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System/DelegateTest.cs  2008-02-12 19:35:19 UTC 
(rev 95548)
+++ trunk/mcs/class/corlib/Test/System/DelegateTest.cs  2008-02-12 19:49:57 UTC 
(rev 95549)
@@ -83,48 +83,68 @@
                }
 
                [Test] // CreateDelegate (Type, Object, String)
-               [Category ("NotWorking")]
                public void CreateDelegate2 ()
                {
                        E e;
 
-                       B b = new B ();
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (), 
"Execute");
+                       Assert.IsNotNull (e, "#A1");
+                       Assert.AreEqual (4, e (new C ()), "#A2");
 
-                       // static method
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (), 
"Execute");
+                       Assert.IsNotNull (e, "#B1");
+                       Assert.AreEqual (4, e (new C ()), "#D2");
+
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (), 
"DoExecute");
+                       Assert.IsNotNull (e, "#C1");
+                       Assert.AreEqual (102, e (new C ()), "#C2");
+               }
+
+               [Test] // CreateDelegate (Type, Object, String)
+               public void CreateDelegate2_Method_ArgumentsMismatch ()
+               {
                        try {
-                               Delegate.CreateDelegate (typeof (E), b, "Run");
-                               Assert.Fail ("#A1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "StartExecute");
+                               Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#A2");
-                               Assert.IsNull (ex.InnerException, "#A3");
-                               Assert.IsNotNull (ex.Message, "#A4");
-                               Assert.IsNull (ex.ParamName, "#A5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
                        }
+               }
 
-                       // matching instance method
-                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"Execute");
-                       Assert.IsNotNull (e, "#B1");
-                       Assert.AreEqual (4, e (new C ()), "#B2");
+               [Test] // CreateDelegate (Type, Object, String)
+               public void CreateDelegate2_Method_CaseMismatch ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), new B (), 
"ExecutE");
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
 
-                       C c = new C ();
-
-                       // static method
+               [Test] // CreateDelegate (Type, Object, String)
+               public void CreateDelegate2_Method_DoesNotExist ()
+               {
                        try {
-                               Delegate.CreateDelegate (typeof (E), c, "Run");
-                               Assert.Fail ("#C1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "DoesNotExist");
+                               Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#C2");
-                               Assert.IsNull (ex.InnerException, "#C3");
-                               Assert.IsNotNull (ex.Message, "#C4");
-                               Assert.IsNull (ex.ParamName, "#C5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
                        }
-
-                       // matching instance method
-                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"Execute");
-                       Assert.IsNotNull (e, "#D1");
-                       Assert.AreEqual (4, e (new C ()), "#D2");
                }
 
                [Test] // CreateDelegate (Type, Object, String)
@@ -144,6 +164,37 @@
                }
 
                [Test] // CreateDelegate (Type, Object, String)
+               public void CreateDelegate2_Method_ReturnTypeMismatch ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "DoExecute");
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Object, String)
+               public void CreateDelegate2_Method_Static ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), new B (), 
"Run");
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Object, String)
                public void CreateDelegate2_Target_Null ()
                {
                        try {
@@ -175,7 +226,6 @@
                }
 
                [Test] // CreateDelegate (Type, Type, String)
-               [Category ("NotWorking")]
                public void CreateDelegate3 ()
                {
                        E e;
@@ -185,37 +235,80 @@
                        Assert.IsNotNull (e, "#A1");
                        Assert.AreEqual (5, e (new C ()), "#A2");
 
-                       // instance method
+                       // matching static method
+                       e = (E) Delegate.CreateDelegate (typeof (E), typeof 
(C), "Run");
+                       Assert.IsNotNull (e, "#B1");
+                       Assert.AreEqual (5, e (new C ()), "#B2");
+
+                       // matching static method
+                       e = (E) Delegate.CreateDelegate (typeof (E), typeof 
(C), "DoRun");
+                       Assert.IsNotNull (e, "#C1");
+                       Assert.AreEqual (107, e (new C ()), "#C2");
+               }
+
+               [Test] // CreateDelegate (Type, Type, String)
+               public void CreateDelegate3_Method_ArgumentsMismatch ()
+               {
                        try {
-                               Delegate.CreateDelegate (typeof (E), typeof 
(B), "Execute");
-                               Assert.Fail ("#B1");
+                               Delegate.CreateDelegate (typeof (E), typeof (B),
+                                       "StartRun");
+                               Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#B2");
-                               Assert.IsNull (ex.InnerException, "#B3");
-                               Assert.IsNotNull (ex.Message, "#B4");
-                               Assert.IsNull (ex.ParamName, "#B5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
                        }
+               }
 
-                       // matching static method
-                       e = (E) Delegate.CreateDelegate (typeof (E), typeof 
(C), "Run");
-                       Assert.IsNotNull (e, "#C1");
-                       Assert.AreEqual (5, e (new C ()), "#C2");
+               [Test] // CreateDelegate (Type, Type, String)
+               public void CreateDelegate3_Method_CaseMismatch ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), typeof 
(B), "RuN");
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
 
-                       // instance method
+               [Test] // CreateDelegate (Type, Type, String)
+               public void CreateDelegate3_Method_DoesNotExist ()
+               {
                        try {
-                               Delegate.CreateDelegate (typeof (E), typeof 
(C), "Execute");
-                               Assert.Fail ("#D1");
+                               Delegate.CreateDelegate (typeof (E), typeof (B),
+                                       "DoesNotExist");
+                               Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#D2");
-                               Assert.IsNull (ex.InnerException, "#D3");
-                               Assert.IsNotNull (ex.Message, "#D4");
-                               Assert.IsNull (ex.ParamName, "#D5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
                        }
                }
 
                [Test] // CreateDelegate (Type, Type, String)
+               public void CreateDelegate3_Method_Instance ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), typeof 
(B), "Execute");
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Type, String)
                public void CreateDelegate3_Method_Null ()
                {
                        try {
@@ -231,6 +324,22 @@
                }
 
                [Test] // CreateDelegate (Type, Type, String)
+               public void CreateDelegate3_Method_ReturnTypeMismatch ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), typeof (B),
+                                       "DoRun");
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Type, String)
                public void CreateDelegate3_Target_Null ()
                {
                        try {
@@ -261,124 +370,105 @@
                }
 
                [Test] // CreateDelegate (Type, Object, String, Boolean)
-               [Category ("NotWorking")]
                public void CreateDelegate4 ()
                {
                        E e;
 
                        B b = new B ();
 
-                       // static method, exact case, ignore case
-                       try {
-                               Delegate.CreateDelegate (typeof (E), b, "Run", 
true);
-                               Assert.Fail ("#A1");
-                       } catch (ArgumentException ex) {
-                               // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#A2");
-                               Assert.IsNull (ex.InnerException, "#A3");
-                               Assert.IsNotNull (ex.Message, "#A4");
-                               Assert.IsNull (ex.ParamName, "#A5");
-                       }
+                       // instance method, exact case, ignore case
+                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"Execute", true);
+                       Assert.IsNotNull (e, "#A1");
+                       Assert.AreEqual (4, e (new C ()), "#A2");
 
-                       // static method, exact case, do not ignore case
-                       try {
-                               Delegate.CreateDelegate (typeof (E), b, "Run", 
false);
-                               Assert.Fail ("#B1");
-                       } catch (ArgumentException ex) {
-                               // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#B2");
-                               Assert.IsNull (ex.InnerException, "#B3");
-                               Assert.IsNotNull (ex.Message, "#B4");
-                               Assert.IsNull (ex.ParamName, "#B5");
-                       }
+                       // instance method, exact case, do not ignore case
+                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"Execute", false);
+                       Assert.IsNotNull (e, "#B1");
+                       Assert.AreEqual (4, e (new C ()), "#B2");
 
-                       // instance method, exact case, ignore case
-                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"Execute", true);
+                       // instance method, case mismatch, ignore case
+                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"ExecutE", true);
                        Assert.IsNotNull (e, "#C1");
                        Assert.AreEqual (4, e (new C ()), "#C2");
 
-                       // instance method, exact case, do not ignore case
-                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"Execute", false);
+                       C c = new C ();
+
+                       // instance method, exact case, ignore case
+                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"Execute", true);
                        Assert.IsNotNull (e, "#D1");
                        Assert.AreEqual (4, e (new C ()), "#D2");
 
-                       // instance method, case mismatch, ignore case
-                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"ExecutE", true);
+                       // instance method, exact case, ignore case
+                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"DoExecute", true);
                        Assert.IsNotNull (e, "#E1");
-                       Assert.AreEqual (4, e (new C ()), "#E2");
+                       Assert.AreEqual (102, e (new C ()), "#E2");
 
-                       // instance method, case mismatch, do not igore case
-                       try {
-                               Delegate.CreateDelegate (typeof (E), b, 
"ExecutE", false);
-                               Assert.Fail ("#F1");
-                       } catch (ArgumentException ex) {
-                               // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#F2");
-                               Assert.IsNull (ex.InnerException, "#F3");
-                               Assert.IsNotNull (ex.Message, "#F4");
-                               Assert.IsNull (ex.ParamName, "#F5");
-                       }
+                       // instance method, exact case, do not ignore case
+                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"Execute", false);
+                       Assert.IsNotNull (e, "#F1");
+                       Assert.AreEqual (4, e (new C ()), "#F2");
 
-                       C c = new C ();
+                       // instance method, case mismatch, ignore case
+                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"ExecutE", true);
+                       Assert.IsNotNull (e, "#G1");
+                       Assert.AreEqual (4, e (new C ()), "#G2");
+               }
 
-                       // static method, exact case, ignore case
+               [Test] // CreateDelegate (Type, Object, String, Boolean)
+               public void CreateDelegate4_Method_ArgumentsMismatch ()
+               {
                        try {
-                               Delegate.CreateDelegate (typeof (E), c, "Run", 
true);
-                               Assert.Fail ("#G1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "StartExecute", false);
+                               Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#G2");
-                               Assert.IsNull (ex.InnerException, "#G3");
-                               Assert.IsNotNull (ex.Message, "#G4");
-                               Assert.IsNull (ex.ParamName, "#G5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
                        }
+               }
 
-                       // static method, exact case, do not ignore case
+               [Test] // CreateDelegate (Type, Object, String, Boolean)
+               public void CreateDelegate4_Method_CaseMismatch ()
+               {
+                       // instance method, case mismatch, do not igore case
                        try {
-                               Delegate.CreateDelegate (typeof (E), c, "Run", 
false);
-                               Assert.Fail ("#H1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "ExecutE", false);
+                               Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#H2");
-                               Assert.IsNull (ex.InnerException, "#H3");
-                               Assert.IsNotNull (ex.Message, "#H4");
-                               Assert.IsNull (ex.ParamName, "#H5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
                        }
+               }
 
-                       // instance method, exact case, ignore case
-                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"Execute", true);
-                       Assert.IsNotNull (e, "#I1");
-                       Assert.AreEqual (4, e (new C ()), "#I2");
-
-                       // instance method, exact case, do not ignore case
-                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"Execute", false);
-                       Assert.IsNotNull (e, "#J1");
-                       Assert.AreEqual (4, e (new C ()), "#J2");
-
-                       // instance method, case mismatch, ignore case
-                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"ExecutE", true);
-                       Assert.IsNotNull (e, "#K1");
-                       Assert.AreEqual (4, e (new C ()), "#K2");
-
-                       // instance method, case mismatch, do not ignore case
+               [Test] // CreateDelegate (Type, Object, String, Boolean)
+               public void CreateDelegate4_Method_DoesNotExist ()
+               {
                        try {
-                               Delegate.CreateDelegate (typeof (E), c, 
"ExecutE", false);
-                               Assert.Fail ("#L1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "DoesNotExist", false);
+                               Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#L2");
-                               Assert.IsNull (ex.InnerException, "#L3");
-                               Assert.IsNotNull (ex.Message, "#L4");
-                               Assert.IsNull (ex.ParamName, "#L5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
                        }
                }
 
                [Test] // CreateDelegate (Type, Object, String, Boolean)
                public void CreateDelegate4_Method_Null ()
                {
-                       C c = new C ();
                        try {
-                               Delegate.CreateDelegate (typeof (D), c, 
(string) null, true);
+                               Delegate.CreateDelegate (typeof (D), new C (),
+                                       (string) null, true);
                                Assert.Fail ("#1");
                        } catch (ArgumentNullException ex) {
                                Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#2");
@@ -390,6 +480,37 @@
                }
 
                [Test] // CreateDelegate (Type, Object, String, Boolean)
+               public void CreateDelegate4_Method_ReturnTypeMismatch ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "DoExecute", false);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Object, String, Boolean)
+               public void CreateDelegate4_Method_Static ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E), new B (), 
"Run", true);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Object, String, Boolean)
                public void CreateDelegate4_Target_Null ()
                {
                        try {
@@ -422,16 +543,90 @@
 
 #if NET_2_0
                [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
-               [Category ("NotWorking")]
-               public void CreateDelegate9_BindFailures ()
+               public void CreateDelegate9 ()
                {
                        E e;
 
-                       B b = new B ();
+                       // do not ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "Execute", false, false);
+                       Assert.IsNotNull (e, "#A1");
+                       Assert.AreEqual (4, e (new C ()), "#A2");
 
-                       // static method, exact case, ignore case, throw on 
bind failure
+                       // do not ignore case, throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "Execute", false, true);
+                       Assert.IsNotNull (e, "#B1");
+                       Assert.AreEqual (4, e (new C ()), "#B2");
+
+                       // ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "Execute", true, false);
+                       Assert.IsNotNull (e, "#C1");
+                       Assert.AreEqual (4, e (new C ()), "#C2");
+
+                       // ignore case, throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "Execute", true, true);
+                       Assert.IsNotNull (e, "#D1");
+                       Assert.AreEqual (4, e (new C ()), "#D2");
+
+                       // do not ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "Execute", false, false);
+                       Assert.IsNotNull (e, "#E1");
+                       Assert.AreEqual (4, e (new C ()), "#E2");
+
+                       // do not ignore case, throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "Execute", false, true);
+                       Assert.IsNotNull (e, "#F1");
+                       Assert.AreEqual (4, e (new C ()), "#F2");
+
+                       // ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "Execute", true, false);
+                       Assert.IsNotNull (e, "#G1");
+                       Assert.AreEqual (4, e (new C ()), "#G2");
+
+                       // ignore case, throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "Execute", true, true);
+                       Assert.IsNotNull (e, "#H1");
+                       Assert.AreEqual (4, e (new C ()), "#H2");
+
+                       // do not ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "DoExecute", false, false);
+                       Assert.IsNotNull (e, "#I1");
+                       Assert.AreEqual (102, e (new C ()), "#I2");
+
+                       // do not ignore case, throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "DoExecute", false, true);
+                       Assert.IsNotNull (e, "#J1");
+                       Assert.AreEqual (102, e (new C ()), "#J2");
+
+                       // ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "DoExecute", true, false);
+                       Assert.IsNotNull (e, "#K1");
+                       Assert.AreEqual (102, e (new C ()), "#K2");
+
+                       // ignore case, throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new C (),
+                               "DoExecute", true, true);
+                       Assert.IsNotNull (e, "#L1");
+                       Assert.AreEqual (102, e (new C ()), "#L2");
+               }
+
+               [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
+               public void CreateDelegate9_Method_ArgumentsMismatch ()
+               {
+                       // throw bind failure
                        try {
-                               Delegate.CreateDelegate (typeof (E), b, "Run", 
true, true);
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "StartExecute", false, true);
                                Assert.Fail ("#A1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
@@ -441,64 +636,162 @@
                                Assert.IsNull (ex.ParamName, "#A5");
                        }
 
-                       // static method, exact case, ignore case, do not throw 
on bind failure
-                       e = (E) Delegate.CreateDelegate (typeof (E), b, "Run", 
true, false);
+                       // do not throw on bind failure
+                       E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "StartExecute", false, false);
                        Assert.IsNull (e, "#B");
+               }
 
-                       // instance method, case mismatch, do not igore case, 
throw on bind failure
+               [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
+               public void CreateDelegate9_Method_CaseMismatch ()
+               {
+                       E e;
+
+                       // do not ignore case, throw bind failure
                        try {
-                               Delegate.CreateDelegate (typeof (E), b, 
"ExecutE",
-                                       false, true);
-                               Assert.Fail ("#C1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "ExecutE", false, true);
+                               Assert.Fail ("#A1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#C2");
-                               Assert.IsNull (ex.InnerException, "#C3");
-                               Assert.IsNotNull (ex.Message, "#C4");
-                               Assert.IsNull (ex.ParamName, "#C5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsNull (ex.ParamName, "#A5");
                        }
 
-                       // instance method, case mismatch, do not igore case, 
do not throw on bind failure
-                       e = (E) Delegate.CreateDelegate (typeof (E), b, 
"ExecutE",
-                               false, false);
-                       Assert.IsNull (e, "#D");
+                       // do not ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "ExecutE", false, false);
+                       Assert.IsNull (e, "#B");
 
-                       C c = new C ();
+                       // ignore case, throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "ExecutE", true, true);
+                       Assert.IsNotNull (e, "#C1");
+                       Assert.AreEqual (4, e (new C ()), "#C2");
 
-                       // static method, exact case, ignore case, throw on 
bind failure
+                       // ignore case, do not throw bind failure
+                       e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "ExecutE", true, false);
+                       Assert.IsNotNull (e, "#D1");
+                       Assert.AreEqual (4, e (new C ()), "#D2");
+               }
+
+               [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
+               public void CreateDelegate9_Method_DoesNotExist ()
+               {
+                       // throw bind failure
                        try {
-                               Delegate.CreateDelegate (typeof (E), c, "Run", 
true, true);
-                               Assert.Fail ("#E1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "DoesNotExist", false, true);
+                               Assert.Fail ("#A1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#E2");
-                               Assert.IsNull (ex.InnerException, "#E3");
-                               Assert.IsNotNull (ex.Message, "#E4");
-                               Assert.IsNull (ex.ParamName, "#E5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsNull (ex.ParamName, "#A5");
                        }
 
-                       // static method, exact case, ignore case, do not throw 
on bind failure
-                       e = (E) Delegate.CreateDelegate (typeof (E), c, "Run", 
true, false);
-                       Assert.IsNull (e, "#F");
+                       // do not throw on bind failure
+                       E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "DoesNotExist", false, false);
+                       Assert.IsNull (e, "#B");
+               }
 
-                       // instance method, case mismatch, do not ignore case, 
throw on bind failure
+               [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
+               public void CreateDelegate9_Method_Null ()
+               {
                        try {
-                               Delegate.CreateDelegate (typeof (E), c, 
"ExecutE", false, true);
-                               Assert.Fail ("#G1");
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       (string) null, false, false);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("method", ex.ParamName, "#6");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
+               public void CreateDelegate9_Method_ReturnTypeMismatch ()
+               {
+                       // throw bind failure
+                       try {
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "DoExecute", false, true);
+                               Assert.Fail ("#A1");
                        } catch (ArgumentException ex) {
                                // Error binding to target method
-                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#G2");
-                               Assert.IsNull (ex.InnerException, "#G3");
-                               Assert.IsNotNull (ex.Message, "#G4");
-                               Assert.IsNull (ex.ParamName, "#G5");
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsNull (ex.ParamName, "#A5");
                        }
 
-                       // instance method, case mismatch, do not ignore case, 
do not throw on bind failure
-                       e = (E) Delegate.CreateDelegate (typeof (E), c, 
"ExecutE",
-                               false, false);
-                       Assert.IsNull (e, "#H");
+                       // do not throw on bind failure
+                       E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "DoExecute", false, false);
+                       Assert.IsNull (e, "#B");
                }
 
+               [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
+               public void CreateDelegate9_Method_Static ()
+               {
+                       // throw bind failure
+                       try {
+                               Delegate.CreateDelegate (typeof (E), new B (),
+                                       "Run", true, true);
+                               Assert.Fail ("#A1");
+                       } catch (ArgumentException ex) {
+                               // Error binding to target method
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsNull (ex.ParamName, "#A5");
+                       }
+
+                       // do not throw on bind failure
+                       E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
+                               "Run", true, false);
+                       Assert.IsNull (e, "#B");
+               }
+
+               [Test] // CreateDelegate (Type, Object, String, Boolean, 
Boolean)
+               public void CreateDelegate9_Target_Null ()
+               {
+                       try {
+                               Delegate.CreateDelegate (typeof (E),(object) 
null,
+                                       "Execute", true, false);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("target", ex.ParamName, "#6");
+                       }
+               }
+
+               [Test] // CreateDelegate (Type, Object, String, Boolean)
+               public void CreateDelegate9_Type_Null ()
+               {
+                       try {
+                               Delegate.CreateDelegate ((Type) null, new B (),
+                                       "Execute", true, false);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentNullException ex) {
+                               Assert.AreEqual (typeof 
(ArgumentNullException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("type", ex.ParamName, "#6");
+                       }
+               }
+
                class ParentClass
                {
                }
@@ -629,10 +922,28 @@
                                return 5;
                        }
 
+                       public static void DoRun (C x)
+                       {
+                       }
+
+                       public static int StartRun (C x, B b)
+                       {
+                               return 6;
+                       }
+
                        int Execute (C c)
                        {
                                return 4;
                        }
+
+                       public void DoExecute (C c)
+                       {
+                       }
+
+                       public int StartExecute (C c, B b)
+                       {
+                               return 3;
+                       }
                }
 
                public class C : B, Iface
@@ -649,6 +960,24 @@
                                return s + "2";
                        }
 
+                       static void Run (C x)
+                       {
+                       }
+
+                       public new static int DoRun (C x)
+                       {
+                               return 107;
+                       }
+
+                       void Execute (C c)
+                       {
+                       }
+
+                       public new int DoExecute (C c)
+                       {
+                               return 102;
+                       }
+
                        public void M ()
                        {
                        }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to