1 // DelegateTest.cs - NUnit Test Cases for the System.Delegate class
3 // (C) Ximian, Inc. http://www.ximian.com
7 using System.Reflection;
8 using System.Reflection.Emit;
9 using System.Threading;
11 using NUnit.Framework;
13 namespace MonoTests.System
16 public class DelegateTest
20 public class GenericClass<T> {
21 public void Method<K> (T t, K k) {}
24 public delegate void SimpleDelegate(int a, double b);
27 [Test] //See bug #372406
28 public void CreateDelegate1_Method_Private_Instance ()
31 MethodInfo mi = typeof (C).GetMethod ("PrivateInstance", BindingFlags.NonPublic | BindingFlags.Instance);
32 Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
33 Assert.AreSame (mi, dg.Method, "#1");
34 Assert.IsNull (dg.Target, "#2");
39 [Test] //Fixes a regression #377324
40 public void GetMethodFromGenericClass ()
42 GenericClass<int> gclass = new GenericClass<int>();
43 SimpleDelegate d = new SimpleDelegate (gclass.Method<double>);
44 MethodInfo method = d.Method;
45 MethodInfo target = typeof (GenericClass<int>).GetMethod ("Method").MakeGenericMethod(typeof(double));
46 Assert.IsNotNull (method, "#1");
47 Assert.AreEqual (target, method, "#2");
51 [Test] // CreateDelegate (Type, MethodInfo)
52 public void CreateDelegate1_Method_Static ()
55 MethodInfo mi = typeof (C).GetMethod ("S");
56 Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
57 Assert.AreSame (mi, dg.Method, "#1");
58 Assert.IsNull (dg.Target, "#2");
63 [Test] // CreateDelegate (Type, MethodInfo)
64 public void CreateDelegate1_Method_Instance ()
67 MethodInfo mi = typeof (C).GetMethod ("M");
69 Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
70 Assert.AreSame (mi, dg.Method, "#1");
71 Assert.IsNull (dg.Target, "#2");
76 Delegate.CreateDelegate (typeof (D), mi);
78 } catch (ArgumentException ex) {
79 // Method must be a static method
80 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
81 Assert.IsNull (ex.InnerException, "#3");
82 Assert.IsNotNull (ex.Message, "#4");
83 Assert.IsNotNull (ex.ParamName, "#5");
84 Assert.AreEqual ("method", ex.ParamName, "#6");
89 [Test] // CreateDelegate (Type, MethodInfo)
90 public void CreateDelegate1_Method_Null ()
93 Delegate.CreateDelegate (typeof (D), (MethodInfo) null);
95 } catch (ArgumentNullException ex) {
96 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
97 Assert.IsNull (ex.InnerException, "#3");
98 Assert.IsNotNull (ex.Message, "#4");
99 Assert.IsNotNull (ex.ParamName, "#5");
100 Assert.AreEqual ("method", ex.ParamName, "#6");
104 [Test] // CreateDelegate (Type, MethodInfo)
105 public void CreateDelegate1_Type_Null ()
107 MethodInfo mi = typeof (C).GetMethod ("S");
109 Delegate.CreateDelegate ((Type) null, mi);
111 } catch (ArgumentNullException ex) {
112 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
113 Assert.IsNull (ex.InnerException, "#3");
114 Assert.IsNotNull (ex.Message, "#4");
115 Assert.IsNotNull (ex.ParamName, "#5");
116 Assert.AreEqual ("type", ex.ParamName, "#6");
120 [Test] // CreateDelegate (Type, Object, String)
121 public void CreateDelegate2 ()
125 e = (E) Delegate.CreateDelegate (typeof (E), new B (), "Execute");
126 Assert.IsNotNull (e, "#A1");
127 Assert.AreEqual (4, e (new C ()), "#A2");
129 e = (E) Delegate.CreateDelegate (typeof (E), new C (), "Execute");
130 Assert.IsNotNull (e, "#B1");
131 Assert.AreEqual (4, e (new C ()), "#D2");
133 e = (E) Delegate.CreateDelegate (typeof (E), new C (), "DoExecute");
134 Assert.IsNotNull (e, "#C1");
135 Assert.AreEqual (102, e (new C ()), "#C2");
138 [Test] // CreateDelegate (Type, Object, String)
139 public void CreateDelegate2_Method_ArgumentsMismatch ()
142 Delegate.CreateDelegate (typeof (E), new B (),
145 } catch (ArgumentException ex) {
146 // Error binding to target method
147 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
148 Assert.IsNull (ex.InnerException, "#3");
149 Assert.IsNotNull (ex.Message, "#4");
150 Assert.IsNull (ex.ParamName, "#5");
154 [Test] // CreateDelegate (Type, Object, String)
155 public void CreateDelegate2_Method_CaseMismatch ()
158 Delegate.CreateDelegate (typeof (E), new B (), "ExecutE");
160 } catch (ArgumentException ex) {
161 // Error binding to target method
162 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
163 Assert.IsNull (ex.InnerException, "#3");
164 Assert.IsNotNull (ex.Message, "#4");
165 Assert.IsNull (ex.ParamName, "#5");
169 [Test] // CreateDelegate (Type, Object, String)
170 public void CreateDelegate2_Method_DoesNotExist ()
173 Delegate.CreateDelegate (typeof (E), new B (),
176 } catch (ArgumentException ex) {
177 // Error binding to target method
178 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
179 Assert.IsNull (ex.InnerException, "#3");
180 Assert.IsNotNull (ex.Message, "#4");
181 Assert.IsNull (ex.ParamName, "#5");
185 [Test] // CreateDelegate (Type, Object, String)
186 public void CreateDelegate2_Method_Null ()
190 Delegate.CreateDelegate (typeof (D), c, (string) null);
192 } catch (ArgumentNullException ex) {
193 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
194 Assert.IsNull (ex.InnerException, "#3");
195 Assert.IsNotNull (ex.Message, "#4");
196 Assert.IsNotNull (ex.ParamName, "#5");
197 Assert.AreEqual ("method", ex.ParamName, "#6");
201 [Test] // CreateDelegate (Type, Object, String)
202 public void CreateDelegate2_Method_ReturnTypeMismatch ()
205 Delegate.CreateDelegate (typeof (E), new B (),
208 } catch (ArgumentException ex) {
209 // Error binding to target method
210 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
211 Assert.IsNull (ex.InnerException, "#3");
212 Assert.IsNotNull (ex.Message, "#4");
213 Assert.IsNull (ex.ParamName, "#5");
217 [Test] // CreateDelegate (Type, Object, String)
218 public void CreateDelegate2_Method_Static ()
221 Delegate.CreateDelegate (typeof (E), new B (), "Run");
223 } catch (ArgumentException ex) {
224 // Error binding to target method
225 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
226 Assert.IsNull (ex.InnerException, "#3");
227 Assert.IsNotNull (ex.Message, "#4");
228 Assert.IsNull (ex.ParamName, "#5");
232 [Test] // CreateDelegate (Type, Object, String)
233 public void CreateDelegate2_Target_Null ()
236 Delegate.CreateDelegate (typeof (D),null, "N");
238 } catch (ArgumentNullException ex) {
239 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
240 Assert.IsNull (ex.InnerException, "#3");
241 Assert.IsNotNull (ex.Message, "#4");
242 Assert.IsNotNull (ex.ParamName, "#5");
243 Assert.AreEqual ("target", ex.ParamName, "#6");
247 [Test] // CreateDelegate (Type, Object, String)
248 public void CreateDelegate2_Type_Null ()
252 Delegate.CreateDelegate ((Type) null, c, "N");
254 } catch (ArgumentNullException ex) {
255 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
256 Assert.IsNull (ex.InnerException, "#3");
257 Assert.IsNotNull (ex.Message, "#4");
258 Assert.IsNotNull (ex.ParamName, "#5");
259 Assert.AreEqual ("type", ex.ParamName, "#6");
263 [Test] // CreateDelegate (Type, Type, String)
264 public void CreateDelegate3 ()
268 // matching static method
269 e = (E) Delegate.CreateDelegate (typeof (E), typeof (B), "Run");
270 Assert.IsNotNull (e, "#A1");
271 Assert.AreEqual (5, e (new C ()), "#A2");
273 // matching static method
274 e = (E) Delegate.CreateDelegate (typeof (E), typeof (C), "Run");
275 Assert.IsNotNull (e, "#B1");
276 Assert.AreEqual (5, e (new C ()), "#B2");
278 // matching static method
279 e = (E) Delegate.CreateDelegate (typeof (E), typeof (C), "DoRun");
280 Assert.IsNotNull (e, "#C1");
281 Assert.AreEqual (107, e (new C ()), "#C2");
284 [Test] // CreateDelegate (Type, Type, String)
285 public void CreateDelegate3_Method_ArgumentsMismatch ()
288 Delegate.CreateDelegate (typeof (E), typeof (B),
291 } catch (ArgumentException ex) {
292 // Error binding to target method
293 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
294 Assert.IsNull (ex.InnerException, "#3");
295 Assert.IsNotNull (ex.Message, "#4");
296 Assert.IsNull (ex.ParamName, "#5");
300 [Test] // CreateDelegate (Type, Type, String)
301 public void CreateDelegate3_Method_CaseMismatch ()
304 Delegate.CreateDelegate (typeof (E), typeof (B), "RuN");
306 } catch (ArgumentException ex) {
307 // Error binding to target method
308 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
309 Assert.IsNull (ex.InnerException, "#3");
310 Assert.IsNotNull (ex.Message, "#4");
311 Assert.IsNull (ex.ParamName, "#5");
315 [Test] // CreateDelegate (Type, Type, String)
316 public void CreateDelegate3_Method_DoesNotExist ()
319 Delegate.CreateDelegate (typeof (E), typeof (B),
322 } catch (ArgumentException ex) {
323 // Error binding to target method
324 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
325 Assert.IsNull (ex.InnerException, "#3");
326 Assert.IsNotNull (ex.Message, "#4");
327 Assert.IsNull (ex.ParamName, "#5");
331 [Test] // CreateDelegate (Type, Type, String)
332 public void CreateDelegate3_Method_Instance ()
335 Delegate.CreateDelegate (typeof (E), typeof (B), "Execute");
337 } catch (ArgumentException ex) {
338 // Error binding to target method
339 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
340 Assert.IsNull (ex.InnerException, "#3");
341 Assert.IsNotNull (ex.Message, "#4");
342 Assert.IsNull (ex.ParamName, "#5");
346 [Test] // CreateDelegate (Type, Type, String)
347 public void CreateDelegate3_Method_Null ()
350 Delegate.CreateDelegate (typeof (D), typeof (C), (string) null);
352 } catch (ArgumentNullException ex) {
353 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
354 Assert.IsNull (ex.InnerException, "#3");
355 Assert.IsNotNull (ex.Message, "#4");
356 Assert.IsNotNull (ex.ParamName, "#5");
357 Assert.AreEqual ("method", ex.ParamName, "#6");
361 [Test] // CreateDelegate (Type, Type, String)
362 public void CreateDelegate3_Method_ReturnTypeMismatch ()
365 Delegate.CreateDelegate (typeof (E), typeof (B),
368 } catch (ArgumentException ex) {
369 // Error binding to target method
370 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
371 Assert.IsNull (ex.InnerException, "#3");
372 Assert.IsNotNull (ex.Message, "#4");
373 Assert.IsNull (ex.ParamName, "#5");
377 [Test] // CreateDelegate (Type, Type, String)
378 public void CreateDelegate3_Target_Null ()
381 Delegate.CreateDelegate (typeof (D), (Type) null, "S");
383 } catch (ArgumentNullException ex) {
384 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
385 Assert.IsNull (ex.InnerException, "#3");
386 Assert.IsNotNull (ex.Message, "#4");
387 Assert.IsNotNull (ex.ParamName, "#5");
388 Assert.AreEqual ("target", ex.ParamName, "#6");
392 [Test] // CreateDelegate (Type, Type, String)
393 public void CreateDelegate3_Type_Null ()
396 Delegate.CreateDelegate ((Type) null, typeof (C), "S");
398 } catch (ArgumentNullException ex) {
399 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
400 Assert.IsNull (ex.InnerException, "#3");
401 Assert.IsNotNull (ex.Message, "#4");
402 Assert.IsNotNull (ex.ParamName, "#5");
403 Assert.AreEqual ("type", ex.ParamName, "#6");
407 [Test] // CreateDelegate (Type, Object, String, Boolean)
408 public void CreateDelegate4 ()
414 // instance method, exact case, ignore case
415 e = (E) Delegate.CreateDelegate (typeof (E), b, "Execute", true);
416 Assert.IsNotNull (e, "#A1");
417 Assert.AreEqual (4, e (new C ()), "#A2");
419 // instance method, exact case, do not ignore case
420 e = (E) Delegate.CreateDelegate (typeof (E), b, "Execute", false);
421 Assert.IsNotNull (e, "#B1");
422 Assert.AreEqual (4, e (new C ()), "#B2");
424 // instance method, case mismatch, ignore case
425 e = (E) Delegate.CreateDelegate (typeof (E), b, "ExecutE", true);
426 Assert.IsNotNull (e, "#C1");
427 Assert.AreEqual (4, e (new C ()), "#C2");
431 // instance method, exact case, ignore case
432 e = (E) Delegate.CreateDelegate (typeof (E), c, "Execute", true);
433 Assert.IsNotNull (e, "#D1");
434 Assert.AreEqual (4, e (new C ()), "#D2");
436 // instance method, exact case, ignore case
437 e = (E) Delegate.CreateDelegate (typeof (E), c, "DoExecute", true);
438 Assert.IsNotNull (e, "#E1");
439 Assert.AreEqual (102, e (new C ()), "#E2");
441 // instance method, exact case, do not ignore case
442 e = (E) Delegate.CreateDelegate (typeof (E), c, "Execute", false);
443 Assert.IsNotNull (e, "#F1");
444 Assert.AreEqual (4, e (new C ()), "#F2");
446 // instance method, case mismatch, ignore case
447 e = (E) Delegate.CreateDelegate (typeof (E), c, "ExecutE", true);
448 Assert.IsNotNull (e, "#G1");
449 Assert.AreEqual (4, e (new C ()), "#G2");
452 [Test] // CreateDelegate (Type, Object, String, Boolean)
453 public void CreateDelegate4_Method_ArgumentsMismatch ()
456 Delegate.CreateDelegate (typeof (E), new B (),
457 "StartExecute", false);
459 } catch (ArgumentException ex) {
460 // Error binding to target method
461 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
462 Assert.IsNull (ex.InnerException, "#3");
463 Assert.IsNotNull (ex.Message, "#4");
464 Assert.IsNull (ex.ParamName, "#5");
468 [Test] // CreateDelegate (Type, Object, String, Boolean)
469 public void CreateDelegate4_Method_CaseMismatch ()
471 // instance method, case mismatch, do not igore case
473 Delegate.CreateDelegate (typeof (E), new B (),
476 } catch (ArgumentException ex) {
477 // Error binding to target method
478 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
479 Assert.IsNull (ex.InnerException, "#3");
480 Assert.IsNotNull (ex.Message, "#4");
481 Assert.IsNull (ex.ParamName, "#5");
485 [Test] // CreateDelegate (Type, Object, String, Boolean)
486 public void CreateDelegate4_Method_DoesNotExist ()
489 Delegate.CreateDelegate (typeof (E), new B (),
490 "DoesNotExist", false);
492 } catch (ArgumentException ex) {
493 // Error binding to target method
494 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
495 Assert.IsNull (ex.InnerException, "#3");
496 Assert.IsNotNull (ex.Message, "#4");
497 Assert.IsNull (ex.ParamName, "#5");
501 [Test] // CreateDelegate (Type, Object, String, Boolean)
502 public void CreateDelegate4_Method_Null ()
505 Delegate.CreateDelegate (typeof (D), new C (),
506 (string) null, true);
508 } catch (ArgumentNullException ex) {
509 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
510 Assert.IsNull (ex.InnerException, "#3");
511 Assert.IsNotNull (ex.Message, "#4");
512 Assert.IsNotNull (ex.ParamName, "#5");
513 Assert.AreEqual ("method", ex.ParamName, "#6");
517 [Test] // CreateDelegate (Type, Object, String, Boolean)
518 public void CreateDelegate4_Method_ReturnTypeMismatch ()
521 Delegate.CreateDelegate (typeof (E), new B (),
524 } catch (ArgumentException ex) {
525 // Error binding to target method
526 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
527 Assert.IsNull (ex.InnerException, "#3");
528 Assert.IsNotNull (ex.Message, "#4");
529 Assert.IsNull (ex.ParamName, "#5");
533 [Test] // CreateDelegate (Type, Object, String, Boolean)
534 public void CreateDelegate4_Method_Static ()
537 Delegate.CreateDelegate (typeof (E), new B (), "Run", true);
539 } catch (ArgumentException ex) {
540 // Error binding to target method
541 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
542 Assert.IsNull (ex.InnerException, "#3");
543 Assert.IsNotNull (ex.Message, "#4");
544 Assert.IsNull (ex.ParamName, "#5");
548 [Test] // CreateDelegate (Type, Object, String, Boolean)
549 public void CreateDelegate4_Target_Null ()
552 Delegate.CreateDelegate (typeof (D), null, "N", true);
554 } catch (ArgumentNullException ex) {
555 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
556 Assert.IsNull (ex.InnerException, "#3");
557 Assert.IsNotNull (ex.Message, "#4");
558 Assert.IsNotNull (ex.ParamName, "#5");
559 Assert.AreEqual ("target", ex.ParamName, "#6");
563 [Test] // CreateDelegate (Type, Object, String, Boolean)
564 public void CreateDelegate4_Type_Null ()
568 Delegate.CreateDelegate ((Type) null, c, "N", true);
570 } catch (ArgumentNullException ex) {
571 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
572 Assert.IsNull (ex.InnerException, "#3");
573 Assert.IsNotNull (ex.Message, "#4");
574 Assert.IsNotNull (ex.ParamName, "#5");
575 Assert.AreEqual ("type", ex.ParamName, "#6");
580 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
581 public void CreateDelegate9 ()
585 // do not ignore case, do not throw bind failure
586 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
587 "Execute", false, false);
588 Assert.IsNotNull (e, "#A1");
589 Assert.AreEqual (4, e (new C ()), "#A2");
591 // do not ignore case, throw bind failure
592 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
593 "Execute", false, true);
594 Assert.IsNotNull (e, "#B1");
595 Assert.AreEqual (4, e (new C ()), "#B2");
597 // ignore case, do not throw bind failure
598 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
599 "Execute", true, false);
600 Assert.IsNotNull (e, "#C1");
601 Assert.AreEqual (4, e (new C ()), "#C2");
603 // ignore case, throw bind failure
604 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
605 "Execute", true, true);
606 Assert.IsNotNull (e, "#D1");
607 Assert.AreEqual (4, e (new C ()), "#D2");
609 // do not ignore case, do not throw bind failure
610 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
611 "Execute", false, false);
612 Assert.IsNotNull (e, "#E1");
613 Assert.AreEqual (4, e (new C ()), "#E2");
615 // do not ignore case, throw bind failure
616 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
617 "Execute", false, true);
618 Assert.IsNotNull (e, "#F1");
619 Assert.AreEqual (4, e (new C ()), "#F2");
621 // ignore case, do not throw bind failure
622 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
623 "Execute", true, false);
624 Assert.IsNotNull (e, "#G1");
625 Assert.AreEqual (4, e (new C ()), "#G2");
627 // ignore case, throw bind failure
628 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
629 "Execute", true, true);
630 Assert.IsNotNull (e, "#H1");
631 Assert.AreEqual (4, e (new C ()), "#H2");
633 // do not ignore case, do not throw bind failure
634 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
635 "DoExecute", false, false);
636 Assert.IsNotNull (e, "#I1");
637 Assert.AreEqual (102, e (new C ()), "#I2");
639 // do not ignore case, throw bind failure
640 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
641 "DoExecute", false, true);
642 Assert.IsNotNull (e, "#J1");
643 Assert.AreEqual (102, e (new C ()), "#J2");
645 // ignore case, do not throw bind failure
646 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
647 "DoExecute", true, false);
648 Assert.IsNotNull (e, "#K1");
649 Assert.AreEqual (102, e (new C ()), "#K2");
651 // ignore case, throw bind failure
652 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
653 "DoExecute", true, true);
654 Assert.IsNotNull (e, "#L1");
655 Assert.AreEqual (102, e (new C ()), "#L2");
658 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
659 public void CreateDelegate9_Method_ArgumentsMismatch ()
661 // throw bind failure
663 Delegate.CreateDelegate (typeof (E), new B (),
664 "StartExecute", false, true);
666 } catch (ArgumentException ex) {
667 // Error binding to target method
668 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
669 Assert.IsNull (ex.InnerException, "#A3");
670 Assert.IsNotNull (ex.Message, "#A4");
671 Assert.IsNull (ex.ParamName, "#A5");
674 // do not throw on bind failure
675 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
676 "StartExecute", false, false);
677 Assert.IsNull (e, "#B");
680 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
681 public void CreateDelegate9_Method_CaseMismatch ()
685 // do not ignore case, throw bind failure
687 Delegate.CreateDelegate (typeof (E), new B (),
688 "ExecutE", false, true);
690 } catch (ArgumentException ex) {
691 // Error binding to target method
692 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
693 Assert.IsNull (ex.InnerException, "#A3");
694 Assert.IsNotNull (ex.Message, "#A4");
695 Assert.IsNull (ex.ParamName, "#A5");
698 // do not ignore case, do not throw bind failure
699 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
700 "ExecutE", false, false);
701 Assert.IsNull (e, "#B");
703 // ignore case, throw bind failure
704 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
705 "ExecutE", true, true);
706 Assert.IsNotNull (e, "#C1");
707 Assert.AreEqual (4, e (new C ()), "#C2");
709 // ignore case, do not throw bind failure
710 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
711 "ExecutE", true, false);
712 Assert.IsNotNull (e, "#D1");
713 Assert.AreEqual (4, e (new C ()), "#D2");
716 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
717 public void CreateDelegate9_Method_DoesNotExist ()
719 // throw bind failure
721 Delegate.CreateDelegate (typeof (E), new B (),
722 "DoesNotExist", false, true);
724 } catch (ArgumentException ex) {
725 // Error binding to target method
726 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
727 Assert.IsNull (ex.InnerException, "#A3");
728 Assert.IsNotNull (ex.Message, "#A4");
729 Assert.IsNull (ex.ParamName, "#A5");
732 // do not throw on bind failure
733 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
734 "DoesNotExist", false, false);
735 Assert.IsNull (e, "#B");
738 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
739 public void CreateDelegate9_Method_Null ()
742 Delegate.CreateDelegate (typeof (E), new B (),
743 (string) null, false, false);
745 } catch (ArgumentNullException ex) {
746 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
747 Assert.IsNull (ex.InnerException, "#3");
748 Assert.IsNotNull (ex.Message, "#4");
749 Assert.IsNotNull (ex.ParamName, "#5");
750 Assert.AreEqual ("method", ex.ParamName, "#6");
754 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
755 public void CreateDelegate9_Method_ReturnTypeMismatch ()
757 // throw bind failure
759 Delegate.CreateDelegate (typeof (E), new B (),
760 "DoExecute", false, true);
762 } catch (ArgumentException ex) {
763 // Error binding to target method
764 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
765 Assert.IsNull (ex.InnerException, "#A3");
766 Assert.IsNotNull (ex.Message, "#A4");
767 Assert.IsNull (ex.ParamName, "#A5");
770 // do not throw on bind failure
771 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
772 "DoExecute", false, false);
773 Assert.IsNull (e, "#B");
776 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
777 public void CreateDelegate9_Method_Static ()
779 // throw bind failure
781 Delegate.CreateDelegate (typeof (E), new B (),
784 } catch (ArgumentException ex) {
785 // Error binding to target method
786 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
787 Assert.IsNull (ex.InnerException, "#A3");
788 Assert.IsNotNull (ex.Message, "#A4");
789 Assert.IsNull (ex.ParamName, "#A5");
792 // do not throw on bind failure
793 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
795 Assert.IsNull (e, "#B");
798 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
799 public void CreateDelegate9_Target_Null ()
802 Delegate.CreateDelegate (typeof (E),(object) null,
803 "Execute", true, false);
805 } catch (ArgumentNullException ex) {
806 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
807 Assert.IsNull (ex.InnerException, "#3");
808 Assert.IsNotNull (ex.Message, "#4");
809 Assert.IsNotNull (ex.ParamName, "#5");
810 Assert.AreEqual ("target", ex.ParamName, "#6");
814 [Test] // CreateDelegate (Type, Object, String, Boolean)
815 public void CreateDelegate9_Type_Null ()
818 Delegate.CreateDelegate ((Type) null, new B (),
819 "Execute", true, false);
821 } catch (ArgumentNullException ex) {
822 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
823 Assert.IsNull (ex.InnerException, "#3");
824 Assert.IsNotNull (ex.Message, "#4");
825 Assert.IsNotNull (ex.ParamName, "#5");
826 Assert.AreEqual ("type", ex.ParamName, "#6");
834 class Subclass : ParentClass
838 delegate ParentClass CoContraVariantDelegate (Subclass s);
840 static Subclass CoContraVariantMethod (ParentClass s)
846 [Category ("TargetJvmNotWorking")]
847 public void CoContraVariance ()
849 CoContraVariantDelegate d = (CoContraVariantDelegate)
850 Delegate.CreateDelegate (typeof (CoContraVariantDelegate),
851 typeof (DelegateTest).GetMethod ("CoContraVariantMethod",
852 BindingFlags.NonPublic|BindingFlags.Static));
856 static object Box (object o)
861 delegate object Boxer ();
864 public void BoxingCovariance ()
866 var boxer = (Boxer) Delegate.CreateDelegate (
869 GetType ().GetMethod ("Box", BindingFlags.NonPublic | BindingFlags.Static));
871 Assert.IsNotNull (boxer);
872 Assert.AreEqual (42, boxer ());
875 static object Nada (int o)
880 delegate int WrongDelegate ();
883 [ExpectedException (typeof (ArgumentException))]
884 public void RemoveDelegateTypeMismatch ()
886 Delegate boxer = new Boxer (() => new object ());
887 Delegate.Remove (boxer, new WrongDelegate (() => 42));
891 [ExpectedException (typeof (ArgumentException))]
892 public void WrongReturnTypeContravariance ()
894 Delegate.CreateDelegate (
895 typeof (WrongDelegate),
897 GetType ().GetMethod ("Nada", BindingFlags.NonPublic | BindingFlags.Static));
900 static int Identity (int i)
906 [ExpectedException (typeof (ArgumentException))]
907 public void WrongReturnTypeContravariance_2 ()
909 Delegate.CreateDelegate (
912 GetType ().GetMethod ("Identity", BindingFlags.NonPublic | BindingFlags.Static));
915 delegate object CallTarget ();
919 static object Target (Closure c)
925 public void NullFirstArgumentOnStaticMethod ()
927 CallTarget call = (CallTarget) Delegate.CreateDelegate (
930 GetType ().GetMethod ("Target", BindingFlags.NonPublic | BindingFlags.Static));
932 Assert.IsNotNull (call);
933 Assert.IsNull (call.Target);
934 Assert.IsNull (call ());
938 public void Virtual ()
940 // Delegate with abstract method, no target
941 FooDelegate del = (FooDelegate)Delegate.CreateDelegate (typeof (FooDelegate), typeof (Iface).GetMethod ("retarg"));
943 Assert.AreEqual ("Hello", del (c, "Hello"));
945 // Combination with normal delegates
946 FooDelegate del2 = (FooDelegate)Delegate.CreateDelegate (typeof (FooDelegate), typeof (Iface).GetMethod ("retarg"));
947 FooDelegate del3 = new FooDelegate (c.retarg2);
948 FooDelegate del4 = (FooDelegate)Delegate.Combine (del2, del3);
950 Assert.AreEqual ("Hello2", del4 (c, "Hello"));
952 // Delegate with virtual method, no target
953 FooDelegate2 del5 = (FooDelegate2)Delegate.CreateDelegate (typeof (FooDelegate2), typeof (B).GetMethod ("retarg3"));
954 Assert.AreEqual ("Hello2", del5 (c, "Hello"));
959 delegate int Del1 (DelegateTest dt, int i);
961 public int method1 (int i) {
962 return int_field + i;
966 public void NullTarget_Instance ()
968 Del1 d = (Del1)Delegate.CreateDelegate (typeof (Del1), null, typeof (DelegateTest).GetMethod ("method1"));
970 DelegateTest dt = new DelegateTest ();
973 Assert.AreEqual (10, d (dt, 5));
976 delegate int Del2 (int i);
978 public static int method2 (int i) {
983 public void NullTarget_Static ()
985 Del2 d = (Del2)Delegate.CreateDelegate (typeof (Del2), null, typeof (DelegateTest).GetMethod ("method2"));
987 Assert.AreEqual (10, d (5));
990 delegate int Del3 (int i);
992 public int method3 (int i) {
993 return int_field + 5;
997 public void HasTarget_Instance ()
999 DelegateTest dt = new DelegateTest ();
1002 Del3 d = (Del3)Delegate.CreateDelegate (typeof (Del3), dt, typeof (DelegateTest).GetMethod ("method3"));
1004 Assert.AreEqual (10, d (5));
1007 delegate int Del4 (int i);
1009 public static int method4 (string s, int i) {
1010 return Int32.Parse (s) + 5;
1014 public void HasTarget_Static ()
1016 Del4 d = (Del4)Delegate.CreateDelegate (typeof (Del4), "5", typeof (DelegateTest).GetMethod ("method4"));
1018 Assert.AreEqual (10, d (5));
1021 public static long? StaticMethodToBeClosedOverNull (object o, long? bar)
1023 Console.WriteLine ("o: {0}", o);
1028 public void ClosedOverNullReferenceStaticMethod ()
1030 var del = (Func<long?,long?>) Delegate.CreateDelegate (
1031 typeof (Func<long?,long?>),
1033 this.GetType ().GetMethod ("StaticMethodToBeClosedOverNull"));
1035 Assert.IsNull (del.Target);
1037 Assert.AreEqual ((long?) 5, del (5));
1040 public void InstanceMethodToBeClosedOverNull ()
1044 public void InstanceMethodIntToBeClosedOverNull (int i)
1049 public void ClosedOverNullReferenceInstanceMethod ()
1051 var action = (Action) Delegate.CreateDelegate (
1054 this.GetType ().GetMethod ("InstanceMethodToBeClosedOverNull"));
1056 Assert.IsNull (action.Target);
1060 var action_int = (Action<int>) Delegate.CreateDelegate (
1061 typeof (Action<int>),
1063 this.GetType ().GetMethod ("InstanceMethodIntToBeClosedOverNull"));
1065 Assert.IsNull (action.Target);
1078 event Action bar_handler;
1081 [ExpectedException (typeof (ArgumentException))] // #635349, #605936
1082 public void NewDelegateClosedOverNullReferenceInstanceMethod ()
1084 bar_handler += foo.Bar;
1087 public void Banga ()
1092 [ExpectedException (typeof (ArgumentException))]
1093 public void CreateDelegateOpenOnly ()
1095 Delegate.CreateDelegate (
1097 this.GetType ().GetMethod ("Banga"));
1101 public void DynamicInvokeNullTarget ()
1103 var method = new DynamicMethod ("test", typeof (int), new [] { typeof (object) }, true);
1104 var il = method.GetILGenerator ();
1105 il.Emit (OpCodes.Ldc_I4, 42);
1106 il.Emit (OpCodes.Ret);
1108 var @delegate = method.CreateDelegate (typeof (Func<int>), null);
1110 Assert.AreEqual (42, (int) @delegate.DynamicInvoke ());
1114 public static void CreateDelegateOfStaticMethodBoundToNull_Helper (object[] args) {}
1117 public void CreateDelegateOfStaticMethodBoundToNull ()
1119 Type t = typeof (Action);
1120 MethodInfo m = typeof (DelegateTest).GetMethod ("CreateDelegateOfStaticMethodBoundToNull_Helper");
1121 object firstArg = null;
1124 Delegate.CreateDelegate (t, m) ;
1126 } catch (ArgumentException) { }
1129 Delegate.CreateDelegate(t, m, true);
1131 } catch (ArgumentException) { }
1134 Delegate.CreateDelegate(t, m, false);
1135 } catch (ArgumentException) { Assert.Fail ("#3"); }
1138 Delegate.CreateDelegate(t, null, m);
1139 } catch (ArgumentException) { Assert.Fail ("#4"); }
1142 Delegate.CreateDelegate(t, null, m, true);
1143 } catch (ArgumentException) { Assert.Fail ("#5"); }
1146 Delegate.CreateDelegate(t, null, m, false);
1147 } catch (ArgumentException) { Assert.Fail ("#6"); }
1151 public void GetHashCode_Constant () {
1152 Action del = delegate {
1154 int hc1 = del.GetHashCode ();
1156 int hc2 = del.GetHashCode ();
1157 Assert.AreEqual (hc1, hc2);
1160 public interface CreateDelegateIFoo {
1164 public abstract class CreateDelegateFoo {
1165 public abstract int Test ();
1168 public class CreateDelegateMid : CreateDelegateFoo {
1169 public override int Test () {
1174 public class CreateDelegateBar : CreateDelegateMid, CreateDelegateIFoo {
1175 public override int Test () {
1179 public int Test2 () {
1184 delegate int IntNoArgs ();
1187 public void CreateDelegateWithAbstractMethods ()
1189 var f = new CreateDelegateBar ();
1190 var m = typeof (CreateDelegateFoo).GetMethod ("Test");
1191 var m2 = typeof (CreateDelegateMid).GetMethod ("Test");
1192 var m3 = typeof (CreateDelegateIFoo).GetMethod ("Test2");
1194 IntNoArgs a1 = (IntNoArgs)Delegate.CreateDelegate (typeof (IntNoArgs), f, m);
1195 IntNoArgs a2 = (IntNoArgs)Delegate.CreateDelegate (typeof (IntNoArgs), f, m2);
1196 IntNoArgs a3 = (IntNoArgs)Delegate.CreateDelegate (typeof (IntNoArgs), f, m3);
1198 Assert.AreEqual (2, a1 (), "#1");
1199 Assert.AreEqual (2, a2 (), "#2");
1200 Assert.AreEqual (3, a3 (), "#3");
1204 delegate string FooDelegate (Iface iface, string s);
1206 delegate string FooDelegate2 (B b, string s);
1208 public interface Iface
1210 string retarg (string s);
1214 public void CreateDelegateWithLdFtnAndAbstractMethod ()
1216 AssemblyName assemblyName = new AssemblyName ();
1217 assemblyName.Name = "customMod";
1218 assemblyName.Version = new Version (1, 2, 3, 4);
1220 AssemblyBuilder assembly
1221 = Thread.GetDomain ().DefineDynamicAssembly (
1222 assemblyName, AssemblyBuilderAccess.RunAndSave);
1224 ModuleBuilder module = assembly.DefineDynamicModule ("res", "res.dll");
1226 TypeBuilder tb = module.DefineType ("Test2", TypeAttributes.Public, typeof (object));
1230 tb.DefineMethod ("test", MethodAttributes.Public | MethodAttributes.Static,
1231 typeof (int), null);
1232 ILGenerator il = mb.GetILGenerator ();
1234 il.Emit (OpCodes.Newobj, typeof (CreateDelegateBar).GetConstructor (new Type [] { }));
1235 il.Emit (OpCodes.Ldftn, typeof (CreateDelegateIFoo).GetMethod ("Test2"));
1236 il.Emit (OpCodes.Newobj, typeof (IntNoArgs).GetConstructor (new Type [] { typeof (object), typeof (IntPtr) }));
1237 il.Emit (OpCodes.Call, typeof (IntNoArgs).GetMethod ("Invoke"));
1238 il.Emit (OpCodes.Ret);
1241 Type t = tb.CreateType ();
1243 Object obj = Activator.CreateInstance (t, new object [0] { });
1245 int a = (int) t.GetMethod ("test").Invoke (obj, null);
1246 Assert.AreEqual (3, a, "#1");
1249 public static int MethodWithIntParam (int x) {
1254 [Category("NotWorking")]
1255 public void CantBindValueTypeToFirstArg () {
1257 Delegate.CreateDelegate (typeof (Delegate695978_2), 10, typeof (DelegateTest).GetMethod ("MethodWithIntParam"));
1258 Assert.Fail ("create delegate must fail");
1259 } catch (ArgumentException) {}
1262 struct Struct695978 {
1264 public int test() { return value + 10; }
1265 public static int test2 (ref Struct695978 foo) { return foo.value + 20; }
1268 delegate int Delegate695978_1 (ref Struct695978 _this);
1269 delegate int Delegate695978_2 ();
1270 delegate int Delegate695978_3 (Struct695978 _this);
1272 [Test] //tests for #695978
1273 public void DelegateWithValueTypeArguments ()
1275 Struct695978 es = new Struct695978 ();
1278 var ar1 = (Delegate695978_1)Delegate.CreateDelegate(typeof (Delegate695978_1), typeof (Struct695978).GetMethod("test"));
1279 Assert.IsNotNull (ar1);
1280 Assert.AreEqual (110, ar1 (ref es), "#1");
1282 var ar2 = (Delegate695978_2)Delegate.CreateDelegate(typeof (Delegate695978_2), null, typeof (Struct695978).GetMethod("test"));
1283 Assert.IsNotNull (ar2);
1286 Assert.Fail ("#i2");
1287 } catch (NullReferenceException) {}
1289 ar1 = (Delegate695978_1) Delegate.CreateDelegate(typeof (Delegate695978_1), typeof (Struct695978).GetMethod("test2"));
1290 Assert.IsNotNull (ar1);
1291 Assert.AreEqual (120, ar1 (ref es), "#3");
1294 Delegate.CreateDelegate(typeof (Delegate695978_2), null, typeof (Struct695978).GetMethod("test2"));
1296 } catch (ArgumentException) {}
1299 ar2 = (Delegate695978_2) Delegate.CreateDelegate(typeof (Delegate695978_2), new Struct695978 () { value = 200 }, typeof (Struct695978).GetMethod("test"));
1300 Assert.IsNotNull (ar2);
1301 Assert.AreEqual (210, ar2 (), "#4");
1304 Delegate.CreateDelegate(typeof (Delegate695978_2), new Struct695978 (), typeof (Struct695978).GetMethod("test2"));
1306 } catch (ArgumentException) {}
1309 Delegate.CreateDelegate(typeof (Delegate695978_3), typeof (Struct695978).GetMethod("test"));
1311 } catch (ArgumentException) {}
1316 public virtual string retarg3 (string s) {
1320 static int Run (C x)
1325 public static void DoRun (C x)
1329 public static int StartRun (C x, B b)
1339 public void DoExecute (C c)
1343 public int StartExecute (C c, B b)
1349 public class C : B, Iface
1351 public string retarg (string s) {
1355 public string retarg2 (Iface iface, string s) {
1359 public override string retarg3 (string s) {
1363 static void Run (C x)
1367 public new static int DoRun (C x)
1376 public new int DoExecute (C c)
1389 public static void S (C c)
1393 private void PrivateInstance ()
1398 public delegate void D (C c);
1399 public delegate int E (C c);