1 // DelegateTest.cs - NUnit Test Cases for the System.Delegate class
3 // (C) Ximian, Inc. http://www.ximian.com
7 using System.Reflection;
9 using System.Reflection.Emit;
11 using System.Threading;
13 using NUnit.Framework;
15 namespace MonoTests.System
18 public class DelegateTest
21 public class GenericClass<T> {
22 public void Method<K> (T t, K k) {}
25 public delegate void SimpleDelegate(int a, double b);
28 [Test] //See bug #372406
30 [Category ("NotWorking")] // #10539
32 public void CreateDelegate1_Method_Private_Instance ()
35 MethodInfo mi = typeof (C).GetMethod ("PrivateInstance", BindingFlags.NonPublic | BindingFlags.Instance);
36 Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
37 Assert.AreSame (mi, dg.Method, "#1");
38 Assert.IsNull (dg.Target, "#2");
43 [Test] //Fixes a regression #377324
44 public void GetMethodFromGenericClass ()
46 GenericClass<int> gclass = new GenericClass<int>();
47 SimpleDelegate d = new SimpleDelegate (gclass.Method<double>);
48 MethodInfo method = d.Method;
49 MethodInfo target = typeof (GenericClass<int>).GetMethod ("Method").MakeGenericMethod(typeof(double));
50 Assert.IsNotNull (method, "#1");
51 Assert.AreEqual (target, method, "#2");
54 [Test] // CreateDelegate (Type, MethodInfo)
55 public void CreateDelegate1_Method_Static ()
58 MethodInfo mi = typeof (C).GetMethod ("S");
59 Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
60 Assert.AreSame (mi, dg.Method, "#1");
61 Assert.IsNull (dg.Target, "#2");
66 [Test] // CreateDelegate (Type, MethodInfo)
68 [Category ("NotWorking")] // #14163
70 public void CreateDelegate1_Method_Instance ()
73 MethodInfo mi = typeof (C).GetMethod ("M");
74 Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
75 Assert.AreSame (mi, dg.Method, "#1");
76 Assert.IsNull (dg.Target, "#2");
81 [Test] // CreateDelegate (Type, MethodInfo)
82 public void CreateDelegate1_Method_Null ()
85 Delegate.CreateDelegate (typeof (D), (MethodInfo) null);
87 } catch (ArgumentNullException ex) {
88 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
89 Assert.IsNull (ex.InnerException, "#3");
90 Assert.IsNotNull (ex.Message, "#4");
91 Assert.IsNotNull (ex.ParamName, "#5");
92 Assert.AreEqual ("method", ex.ParamName, "#6");
96 [Test] // CreateDelegate (Type, MethodInfo)
97 public void CreateDelegate1_Type_Null ()
99 MethodInfo mi = typeof (C).GetMethod ("S");
101 Delegate.CreateDelegate ((Type) null, mi);
103 } catch (ArgumentNullException ex) {
104 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
105 Assert.IsNull (ex.InnerException, "#3");
106 Assert.IsNotNull (ex.Message, "#4");
107 Assert.IsNotNull (ex.ParamName, "#5");
108 Assert.AreEqual ("type", ex.ParamName, "#6");
112 [Test] // CreateDelegate (Type, Object, String)
113 public void CreateDelegate2 ()
117 e = (E) Delegate.CreateDelegate (typeof (E), new B (), "Execute");
118 Assert.IsNotNull (e, "#A1");
119 Assert.AreEqual (4, e (new C ()), "#A2");
121 e = (E) Delegate.CreateDelegate (typeof (E), new C (), "Execute");
122 Assert.IsNotNull (e, "#B1");
123 Assert.AreEqual (4, e (new C ()), "#D2");
125 e = (E) Delegate.CreateDelegate (typeof (E), new C (), "DoExecute");
126 Assert.IsNotNull (e, "#C1");
127 Assert.AreEqual (102, e (new C ()), "#C2");
130 [Test] // CreateDelegate (Type, Object, String)
131 public void CreateDelegate2_Method_ArgumentsMismatch ()
134 Delegate.CreateDelegate (typeof (E), new B (),
137 } catch (ArgumentException ex) {
138 // Error binding to target method
139 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
140 Assert.IsNull (ex.InnerException, "#3");
141 Assert.IsNotNull (ex.Message, "#4");
142 Assert.IsNull (ex.ParamName, "#5");
146 [Test] // CreateDelegate (Type, Object, String)
147 public void CreateDelegate2_Method_CaseMismatch ()
150 Delegate.CreateDelegate (typeof (E), new B (), "ExecutE");
152 } catch (ArgumentException ex) {
153 // Error binding to target method
154 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
155 Assert.IsNull (ex.InnerException, "#3");
156 Assert.IsNotNull (ex.Message, "#4");
157 Assert.IsNull (ex.ParamName, "#5");
161 [Test] // CreateDelegate (Type, Object, String)
162 public void CreateDelegate2_Method_DoesNotExist ()
165 Delegate.CreateDelegate (typeof (E), new B (),
168 } catch (ArgumentException ex) {
169 // Error binding to target method
170 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
171 Assert.IsNull (ex.InnerException, "#3");
172 Assert.IsNotNull (ex.Message, "#4");
173 Assert.IsNull (ex.ParamName, "#5");
177 [Test] // CreateDelegate (Type, Object, String)
178 public void CreateDelegate2_Method_Null ()
182 Delegate.CreateDelegate (typeof (D), c, (string) null);
184 } catch (ArgumentNullException ex) {
185 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
186 Assert.IsNull (ex.InnerException, "#3");
187 Assert.IsNotNull (ex.Message, "#4");
188 Assert.IsNotNull (ex.ParamName, "#5");
189 Assert.AreEqual ("method", ex.ParamName, "#6");
193 [Test] // CreateDelegate (Type, Object, String)
194 public void CreateDelegate2_Method_ReturnTypeMismatch ()
197 Delegate.CreateDelegate (typeof (E), new B (),
200 } catch (ArgumentException ex) {
201 // Error binding to target method
202 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
203 Assert.IsNull (ex.InnerException, "#3");
204 Assert.IsNotNull (ex.Message, "#4");
205 Assert.IsNull (ex.ParamName, "#5");
209 [Test] // CreateDelegate (Type, Object, String)
210 public void CreateDelegate2_Method_Static ()
213 Delegate.CreateDelegate (typeof (E), new B (), "Run");
215 } catch (ArgumentException ex) {
216 // Error binding to target method
217 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
218 Assert.IsNull (ex.InnerException, "#3");
219 Assert.IsNotNull (ex.Message, "#4");
220 Assert.IsNull (ex.ParamName, "#5");
224 [Test] // CreateDelegate (Type, Object, String)
225 public void CreateDelegate2_Target_Null ()
228 Delegate.CreateDelegate (typeof (D),null, "N");
230 } catch (ArgumentNullException ex) {
231 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
232 Assert.IsNull (ex.InnerException, "#3");
233 Assert.IsNotNull (ex.Message, "#4");
234 Assert.IsNotNull (ex.ParamName, "#5");
235 Assert.AreEqual ("target", ex.ParamName, "#6");
239 [Test] // CreateDelegate (Type, Object, String)
240 public void CreateDelegate2_Type_Null ()
244 Delegate.CreateDelegate ((Type) null, c, "N");
246 } catch (ArgumentNullException ex) {
247 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
248 Assert.IsNull (ex.InnerException, "#3");
249 Assert.IsNotNull (ex.Message, "#4");
250 Assert.IsNotNull (ex.ParamName, "#5");
251 Assert.AreEqual ("type", ex.ParamName, "#6");
255 [Test] // CreateDelegate (Type, Type, String)
256 public void CreateDelegate3 ()
260 // matching static method
261 e = (E) Delegate.CreateDelegate (typeof (E), typeof (B), "Run");
262 Assert.IsNotNull (e, "#A1");
263 Assert.AreEqual (5, e (new C ()), "#A2");
265 // matching static method
266 e = (E) Delegate.CreateDelegate (typeof (E), typeof (C), "Run");
267 Assert.IsNotNull (e, "#B1");
268 Assert.AreEqual (5, e (new C ()), "#B2");
270 // matching static method
271 e = (E) Delegate.CreateDelegate (typeof (E), typeof (C), "DoRun");
272 Assert.IsNotNull (e, "#C1");
273 Assert.AreEqual (107, e (new C ()), "#C2");
276 [Test] // CreateDelegate (Type, Type, String)
277 public void CreateDelegate3_Method_ArgumentsMismatch ()
280 Delegate.CreateDelegate (typeof (E), typeof (B),
283 } catch (ArgumentException ex) {
284 // Error binding to target method
285 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
286 Assert.IsNull (ex.InnerException, "#3");
287 Assert.IsNotNull (ex.Message, "#4");
288 Assert.IsNull (ex.ParamName, "#5");
292 [Test] // CreateDelegate (Type, Type, String)
293 public void CreateDelegate3_Method_CaseMismatch ()
296 Delegate.CreateDelegate (typeof (E), typeof (B), "RuN");
298 } catch (ArgumentException ex) {
299 // Error binding to target method
300 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
301 Assert.IsNull (ex.InnerException, "#3");
302 Assert.IsNotNull (ex.Message, "#4");
303 Assert.IsNull (ex.ParamName, "#5");
307 [Test] // CreateDelegate (Type, Type, String)
308 public void CreateDelegate3_Method_DoesNotExist ()
311 Delegate.CreateDelegate (typeof (E), typeof (B),
314 } catch (ArgumentException ex) {
315 // Error binding to target method
316 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
317 Assert.IsNull (ex.InnerException, "#3");
318 Assert.IsNotNull (ex.Message, "#4");
319 Assert.IsNull (ex.ParamName, "#5");
323 [Test] // CreateDelegate (Type, Type, String)
324 public void CreateDelegate3_Method_Instance ()
327 Delegate.CreateDelegate (typeof (E), typeof (B), "Execute");
329 } catch (ArgumentException ex) {
330 // Error binding to target method
331 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
332 Assert.IsNull (ex.InnerException, "#3");
333 Assert.IsNotNull (ex.Message, "#4");
334 Assert.IsNull (ex.ParamName, "#5");
338 [Test] // CreateDelegate (Type, Type, String)
339 public void CreateDelegate3_Method_Null ()
342 Delegate.CreateDelegate (typeof (D), typeof (C), (string) null);
344 } catch (ArgumentNullException ex) {
345 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
346 Assert.IsNull (ex.InnerException, "#3");
347 Assert.IsNotNull (ex.Message, "#4");
348 Assert.IsNotNull (ex.ParamName, "#5");
349 Assert.AreEqual ("method", ex.ParamName, "#6");
353 [Test] // CreateDelegate (Type, Type, String)
354 public void CreateDelegate3_Method_ReturnTypeMismatch ()
357 Delegate.CreateDelegate (typeof (E), typeof (B),
360 } catch (ArgumentException ex) {
361 // Error binding to target method
362 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
363 Assert.IsNull (ex.InnerException, "#3");
364 Assert.IsNotNull (ex.Message, "#4");
365 Assert.IsNull (ex.ParamName, "#5");
369 [Test] // CreateDelegate (Type, Type, String)
370 public void CreateDelegate3_Target_Null ()
373 Delegate.CreateDelegate (typeof (D), (Type) null, "S");
375 } catch (ArgumentNullException ex) {
376 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
377 Assert.IsNull (ex.InnerException, "#3");
378 Assert.IsNotNull (ex.Message, "#4");
379 Assert.IsNotNull (ex.ParamName, "#5");
380 Assert.AreEqual ("target", ex.ParamName, "#6");
384 [Test] // CreateDelegate (Type, Type, String)
385 public void CreateDelegate3_Type_Null ()
388 Delegate.CreateDelegate ((Type) null, typeof (C), "S");
390 } catch (ArgumentNullException ex) {
391 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
392 Assert.IsNull (ex.InnerException, "#3");
393 Assert.IsNotNull (ex.Message, "#4");
394 Assert.IsNotNull (ex.ParamName, "#5");
395 Assert.AreEqual ("type", ex.ParamName, "#6");
399 [Test] // CreateDelegate (Type, Object, String, Boolean)
400 public void CreateDelegate4 ()
406 // instance method, exact case, ignore case
407 e = (E) Delegate.CreateDelegate (typeof (E), b, "Execute", true);
408 Assert.IsNotNull (e, "#A1");
409 Assert.AreEqual (4, e (new C ()), "#A2");
411 // instance method, exact case, do not ignore case
412 e = (E) Delegate.CreateDelegate (typeof (E), b, "Execute", false);
413 Assert.IsNotNull (e, "#B1");
414 Assert.AreEqual (4, e (new C ()), "#B2");
416 // instance method, case mismatch, ignore case
417 e = (E) Delegate.CreateDelegate (typeof (E), b, "ExecutE", true);
418 Assert.IsNotNull (e, "#C1");
419 Assert.AreEqual (4, e (new C ()), "#C2");
423 // instance method, exact case, ignore case
424 e = (E) Delegate.CreateDelegate (typeof (E), c, "Execute", true);
425 Assert.IsNotNull (e, "#D1");
426 Assert.AreEqual (4, e (new C ()), "#D2");
428 // instance method, exact case, ignore case
429 e = (E) Delegate.CreateDelegate (typeof (E), c, "DoExecute", true);
430 Assert.IsNotNull (e, "#E1");
431 Assert.AreEqual (102, e (new C ()), "#E2");
433 // instance method, exact case, do not ignore case
434 e = (E) Delegate.CreateDelegate (typeof (E), c, "Execute", false);
435 Assert.IsNotNull (e, "#F1");
436 Assert.AreEqual (4, e (new C ()), "#F2");
438 // instance method, case mismatch, ignore case
439 e = (E) Delegate.CreateDelegate (typeof (E), c, "ExecutE", true);
440 Assert.IsNotNull (e, "#G1");
441 Assert.AreEqual (4, e (new C ()), "#G2");
444 [Test] // CreateDelegate (Type, Object, String, Boolean)
445 public void CreateDelegate4_Method_ArgumentsMismatch ()
448 Delegate.CreateDelegate (typeof (E), new B (),
449 "StartExecute", false);
451 } catch (ArgumentException ex) {
452 // Error binding to target method
453 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
454 Assert.IsNull (ex.InnerException, "#3");
455 Assert.IsNotNull (ex.Message, "#4");
456 Assert.IsNull (ex.ParamName, "#5");
460 [Test] // CreateDelegate (Type, Object, String, Boolean)
461 public void CreateDelegate4_Method_CaseMismatch ()
463 // instance method, case mismatch, do not igore case
465 Delegate.CreateDelegate (typeof (E), new B (),
468 } catch (ArgumentException ex) {
469 // Error binding to target method
470 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
471 Assert.IsNull (ex.InnerException, "#3");
472 Assert.IsNotNull (ex.Message, "#4");
473 Assert.IsNull (ex.ParamName, "#5");
477 [Test] // CreateDelegate (Type, Object, String, Boolean)
478 public void CreateDelegate4_Method_DoesNotExist ()
481 Delegate.CreateDelegate (typeof (E), new B (),
482 "DoesNotExist", false);
484 } catch (ArgumentException ex) {
485 // Error binding to target method
486 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
487 Assert.IsNull (ex.InnerException, "#3");
488 Assert.IsNotNull (ex.Message, "#4");
489 Assert.IsNull (ex.ParamName, "#5");
493 [Test] // CreateDelegate (Type, Object, String, Boolean)
494 public void CreateDelegate4_Method_Null ()
497 Delegate.CreateDelegate (typeof (D), new C (),
498 (string) null, true);
500 } catch (ArgumentNullException ex) {
501 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
502 Assert.IsNull (ex.InnerException, "#3");
503 Assert.IsNotNull (ex.Message, "#4");
504 Assert.IsNotNull (ex.ParamName, "#5");
505 Assert.AreEqual ("method", ex.ParamName, "#6");
509 [Test] // CreateDelegate (Type, Object, String, Boolean)
510 public void CreateDelegate4_Method_ReturnTypeMismatch ()
513 Delegate.CreateDelegate (typeof (E), new B (),
516 } catch (ArgumentException ex) {
517 // Error binding to target method
518 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
519 Assert.IsNull (ex.InnerException, "#3");
520 Assert.IsNotNull (ex.Message, "#4");
521 Assert.IsNull (ex.ParamName, "#5");
525 [Test] // CreateDelegate (Type, Object, String, Boolean)
526 public void CreateDelegate4_Method_Static ()
529 Delegate.CreateDelegate (typeof (E), new B (), "Run", true);
531 } catch (ArgumentException ex) {
532 // Error binding to target method
533 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
534 Assert.IsNull (ex.InnerException, "#3");
535 Assert.IsNotNull (ex.Message, "#4");
536 Assert.IsNull (ex.ParamName, "#5");
540 [Test] // CreateDelegate (Type, Object, String, Boolean)
541 public void CreateDelegate4_Target_Null ()
544 Delegate.CreateDelegate (typeof (D), null, "N", true);
546 } catch (ArgumentNullException ex) {
547 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
548 Assert.IsNull (ex.InnerException, "#3");
549 Assert.IsNotNull (ex.Message, "#4");
550 Assert.IsNotNull (ex.ParamName, "#5");
551 Assert.AreEqual ("target", ex.ParamName, "#6");
555 [Test] // CreateDelegate (Type, Object, String, Boolean)
556 public void CreateDelegate4_Type_Null ()
560 Delegate.CreateDelegate ((Type) null, c, "N", true);
562 } catch (ArgumentNullException ex) {
563 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
564 Assert.IsNull (ex.InnerException, "#3");
565 Assert.IsNotNull (ex.Message, "#4");
566 Assert.IsNotNull (ex.ParamName, "#5");
567 Assert.AreEqual ("type", ex.ParamName, "#6");
571 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
572 public void CreateDelegate9 ()
576 // do not ignore case, do not throw bind failure
577 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
578 "Execute", false, false);
579 Assert.IsNotNull (e, "#A1");
580 Assert.AreEqual (4, e (new C ()), "#A2");
582 // do not ignore case, throw bind failure
583 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
584 "Execute", false, true);
585 Assert.IsNotNull (e, "#B1");
586 Assert.AreEqual (4, e (new C ()), "#B2");
588 // ignore case, do not throw bind failure
589 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
590 "Execute", true, false);
591 Assert.IsNotNull (e, "#C1");
592 Assert.AreEqual (4, e (new C ()), "#C2");
594 // ignore case, throw bind failure
595 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
596 "Execute", true, true);
597 Assert.IsNotNull (e, "#D1");
598 Assert.AreEqual (4, e (new C ()), "#D2");
600 // do not ignore case, do not throw bind failure
601 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
602 "Execute", false, false);
603 Assert.IsNotNull (e, "#E1");
604 Assert.AreEqual (4, e (new C ()), "#E2");
606 // do not ignore case, throw bind failure
607 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
608 "Execute", false, true);
609 Assert.IsNotNull (e, "#F1");
610 Assert.AreEqual (4, e (new C ()), "#F2");
612 // ignore case, do not throw bind failure
613 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
614 "Execute", true, false);
615 Assert.IsNotNull (e, "#G1");
616 Assert.AreEqual (4, e (new C ()), "#G2");
618 // ignore case, throw bind failure
619 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
620 "Execute", true, true);
621 Assert.IsNotNull (e, "#H1");
622 Assert.AreEqual (4, e (new C ()), "#H2");
624 // do not ignore case, do not throw bind failure
625 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
626 "DoExecute", false, false);
627 Assert.IsNotNull (e, "#I1");
628 Assert.AreEqual (102, e (new C ()), "#I2");
630 // do not ignore case, throw bind failure
631 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
632 "DoExecute", false, true);
633 Assert.IsNotNull (e, "#J1");
634 Assert.AreEqual (102, e (new C ()), "#J2");
636 // ignore case, do not throw bind failure
637 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
638 "DoExecute", true, false);
639 Assert.IsNotNull (e, "#K1");
640 Assert.AreEqual (102, e (new C ()), "#K2");
642 // ignore case, throw bind failure
643 e = (E) Delegate.CreateDelegate (typeof (E), new C (),
644 "DoExecute", true, true);
645 Assert.IsNotNull (e, "#L1");
646 Assert.AreEqual (102, e (new C ()), "#L2");
649 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
650 public void CreateDelegate9_Method_ArgumentsMismatch ()
652 // throw bind failure
654 Delegate.CreateDelegate (typeof (E), new B (),
655 "StartExecute", false, true);
657 } catch (ArgumentException ex) {
658 // Error binding to target method
659 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
660 Assert.IsNull (ex.InnerException, "#A3");
661 Assert.IsNotNull (ex.Message, "#A4");
662 Assert.IsNull (ex.ParamName, "#A5");
665 // do not throw on bind failure
666 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
667 "StartExecute", false, false);
668 Assert.IsNull (e, "#B");
671 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
672 public void CreateDelegate9_Method_CaseMismatch ()
676 // do not ignore case, throw bind failure
678 Delegate.CreateDelegate (typeof (E), new B (),
679 "ExecutE", false, true);
681 } catch (ArgumentException ex) {
682 // Error binding to target method
683 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
684 Assert.IsNull (ex.InnerException, "#A3");
685 Assert.IsNotNull (ex.Message, "#A4");
686 Assert.IsNull (ex.ParamName, "#A5");
689 // do not ignore case, do not throw bind failure
690 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
691 "ExecutE", false, false);
692 Assert.IsNull (e, "#B");
694 // ignore case, throw bind failure
695 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
696 "ExecutE", true, true);
697 Assert.IsNotNull (e, "#C1");
698 Assert.AreEqual (4, e (new C ()), "#C2");
700 // ignore case, do not throw bind failure
701 e = (E) Delegate.CreateDelegate (typeof (E), new B (),
702 "ExecutE", true, false);
703 Assert.IsNotNull (e, "#D1");
704 Assert.AreEqual (4, e (new C ()), "#D2");
707 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
708 public void CreateDelegate9_Method_DoesNotExist ()
710 // throw bind failure
712 Delegate.CreateDelegate (typeof (E), new B (),
713 "DoesNotExist", false, true);
715 } catch (ArgumentException ex) {
716 // Error binding to target method
717 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
718 Assert.IsNull (ex.InnerException, "#A3");
719 Assert.IsNotNull (ex.Message, "#A4");
720 Assert.IsNull (ex.ParamName, "#A5");
723 // do not throw on bind failure
724 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
725 "DoesNotExist", false, false);
726 Assert.IsNull (e, "#B");
729 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
730 public void CreateDelegate9_Method_Null ()
733 Delegate.CreateDelegate (typeof (E), new B (),
734 (string) null, false, false);
736 } catch (ArgumentNullException ex) {
737 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
738 Assert.IsNull (ex.InnerException, "#3");
739 Assert.IsNotNull (ex.Message, "#4");
740 Assert.IsNotNull (ex.ParamName, "#5");
741 Assert.AreEqual ("method", ex.ParamName, "#6");
745 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
746 public void CreateDelegate9_Method_ReturnTypeMismatch ()
748 // throw bind failure
750 Delegate.CreateDelegate (typeof (E), new B (),
751 "DoExecute", false, true);
753 } catch (ArgumentException ex) {
754 // Error binding to target method
755 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
756 Assert.IsNull (ex.InnerException, "#A3");
757 Assert.IsNotNull (ex.Message, "#A4");
758 Assert.IsNull (ex.ParamName, "#A5");
761 // do not throw on bind failure
762 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
763 "DoExecute", false, false);
764 Assert.IsNull (e, "#B");
767 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
768 public void CreateDelegate9_Method_Static ()
770 // throw bind failure
772 Delegate.CreateDelegate (typeof (E), new B (),
775 } catch (ArgumentException ex) {
776 // Error binding to target method
777 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
778 Assert.IsNull (ex.InnerException, "#A3");
779 Assert.IsNotNull (ex.Message, "#A4");
780 Assert.IsNull (ex.ParamName, "#A5");
783 // do not throw on bind failure
784 E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
786 Assert.IsNull (e, "#B");
789 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
790 public void CreateDelegate9_Target_Null ()
793 Delegate.CreateDelegate (typeof (E),(object) null,
794 "Execute", true, false);
796 } catch (ArgumentNullException ex) {
797 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
798 Assert.IsNull (ex.InnerException, "#3");
799 Assert.IsNotNull (ex.Message, "#4");
800 Assert.IsNotNull (ex.ParamName, "#5");
801 Assert.AreEqual ("target", ex.ParamName, "#6");
805 [Test] // CreateDelegate (Type, Object, String, Boolean)
806 public void CreateDelegate9_Type_Null ()
809 Delegate.CreateDelegate ((Type) null, new B (),
810 "Execute", true, false);
812 } catch (ArgumentNullException ex) {
813 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
814 Assert.IsNull (ex.InnerException, "#3");
815 Assert.IsNotNull (ex.Message, "#4");
816 Assert.IsNotNull (ex.ParamName, "#5");
817 Assert.AreEqual ("type", ex.ParamName, "#6");
825 class Subclass : ParentClass
829 delegate ParentClass CoContraVariantDelegate (Subclass s);
831 static Subclass CoContraVariantMethod (ParentClass s)
837 public void CoContraVariance ()
839 CoContraVariantDelegate d = (CoContraVariantDelegate)
840 Delegate.CreateDelegate (typeof (CoContraVariantDelegate),
841 typeof (DelegateTest).GetMethod ("CoContraVariantMethod",
842 BindingFlags.NonPublic|BindingFlags.Static));
846 static object Box (object o)
851 delegate object Boxer ();
854 public void BoxingCovariance ()
856 var boxer = (Boxer) Delegate.CreateDelegate (
859 GetType ().GetMethod ("Box", BindingFlags.NonPublic | BindingFlags.Static));
861 Assert.IsNotNull (boxer);
862 Assert.AreEqual (42, boxer ());
865 static object Nada (int o)
870 delegate int WrongDelegate ();
873 [ExpectedException (typeof (ArgumentException))]
874 public void RemoveDelegateTypeMismatch ()
876 Delegate boxer = new Boxer (() => new object ());
877 Delegate.Remove (boxer, new WrongDelegate (() => 42));
881 [ExpectedException (typeof (ArgumentException))]
882 public void WrongReturnTypeContravariance ()
884 Delegate.CreateDelegate (
885 typeof (WrongDelegate),
887 GetType ().GetMethod ("Nada", BindingFlags.NonPublic | BindingFlags.Static));
890 static int Identity (int i)
896 [ExpectedException (typeof (ArgumentException))]
897 public void WrongReturnTypeContravariance_2 ()
899 Delegate.CreateDelegate (
902 GetType ().GetMethod ("Identity", BindingFlags.NonPublic | BindingFlags.Static));
905 delegate object CallTarget ();
909 static object Target (Closure c)
915 public void NullFirstArgumentOnStaticMethod ()
917 CallTarget call = (CallTarget) Delegate.CreateDelegate (
920 GetType ().GetMethod ("Target", BindingFlags.NonPublic | BindingFlags.Static));
922 Assert.IsNotNull (call);
923 Assert.IsNull (call.Target);
924 Assert.IsNull (call ());
929 [Category ("NotWorking")] // #10539
931 public void Virtual ()
933 // Delegate with abstract method, no target
934 FooDelegate del = (FooDelegate)Delegate.CreateDelegate (typeof (FooDelegate), typeof (Iface).GetMethod ("retarg"));
936 Assert.AreEqual ("Hello", del (c, "Hello"));
938 // Combination with normal delegates
939 FooDelegate del2 = (FooDelegate)Delegate.CreateDelegate (typeof (FooDelegate), typeof (Iface).GetMethod ("retarg"));
940 FooDelegate del3 = new FooDelegate (c.retarg2);
941 FooDelegate del4 = (FooDelegate)Delegate.Combine (del2, del3);
943 Assert.AreEqual ("Hello2", del4 (c, "Hello"));
945 // Delegate with virtual method, no target
946 FooDelegate2 del5 = (FooDelegate2)Delegate.CreateDelegate (typeof (FooDelegate2), typeof (B).GetMethod ("retarg3"));
947 Assert.AreEqual ("Hello2", del5 (c, "Hello"));
952 delegate int Del1 (DelegateTest dt, int i);
954 public int method1 (int i) {
955 return int_field + i;
960 [Category ("NotWorking")] // #14163
962 public void NullTarget_Instance ()
964 Del1 d = (Del1)Delegate.CreateDelegate (typeof (Del1), null, typeof (DelegateTest).GetMethod ("method1"));
966 DelegateTest dt = new DelegateTest ();
969 Assert.AreEqual (10, d (dt, 5));
972 delegate int Del2 (int i);
974 public static int method2 (int i) {
979 public void NullTarget_Static ()
981 Del2 d = (Del2)Delegate.CreateDelegate (typeof (Del2), null, typeof (DelegateTest).GetMethod ("method2"));
983 Assert.AreEqual (10, d (5));
986 delegate int Del3 (int i);
988 public int method3 (int i) {
989 return int_field + 5;
993 public void HasTarget_Instance ()
995 DelegateTest dt = new DelegateTest ();
998 Del3 d = (Del3)Delegate.CreateDelegate (typeof (Del3), dt, typeof (DelegateTest).GetMethod ("method3"));
1000 Assert.AreEqual (10, d (5));
1003 delegate int Del4 (int i);
1005 public static int method4 (string s, int i) {
1006 return Int32.Parse (s) + 5;
1010 public void HasTarget_Static ()
1012 Del4 d = (Del4)Delegate.CreateDelegate (typeof (Del4), "5", typeof (DelegateTest).GetMethod ("method4"));
1014 Assert.AreEqual (10, d (5));
1017 public static long? StaticMethodToBeClosedOverNull (object o, long? bar)
1019 Console.WriteLine ("o: {0}", o);
1025 [Category ("NotWorking")] // #10539
1027 public void ClosedOverNullReferenceStaticMethod ()
1029 var del = (Func<long?,long?>) Delegate.CreateDelegate (
1030 typeof (Func<long?,long?>),
1032 this.GetType ().GetMethod ("StaticMethodToBeClosedOverNull"));
1034 Assert.IsNull (del.Target);
1036 Assert.AreEqual ((long?) 5, del (5));
1039 public void InstanceMethodToBeClosedOverNull ()
1043 public void InstanceMethodIntToBeClosedOverNull (int i)
1049 [Category ("NotWorking")] // #10539
1051 public void ClosedOverNullReferenceInstanceMethod ()
1053 var action = (Action) Delegate.CreateDelegate (
1056 this.GetType ().GetMethod ("InstanceMethodToBeClosedOverNull"));
1058 Assert.IsNull (action.Target);
1062 var action_int = (Action<int>) Delegate.CreateDelegate (
1063 typeof (Action<int>),
1065 this.GetType ().GetMethod ("InstanceMethodIntToBeClosedOverNull"));
1067 Assert.IsNull (action.Target);
1073 public int i, j, k, l;
1075 public int GetProp (int a, int b, int c, int d) {
1080 delegate int ByRefDelegate (ref FooStruct s, int a, int b, int c, int d);
1083 [Category ("NotWorking")]
1086 public void CallVirtVType ()
1088 var action = (ByRefDelegate)Delegate.CreateDelegate (typeof (ByRefDelegate), null, typeof (FooStruct).GetMethod ("GetProp"));
1089 var s = new FooStruct () { i = 42 };
1090 Assert.AreEqual (42, action (ref s, 1, 2, 3, 4));
1101 event Action bar_handler;
1104 [ExpectedException (typeof (ArgumentException))] // #635349, #605936
1105 public void NewDelegateClosedOverNullReferenceInstanceMethod ()
1107 bar_handler += foo.Bar;
1110 public void Banga ()
1115 [ExpectedException (typeof (ArgumentException))]
1116 public void CreateDelegateOpenOnly ()
1118 Delegate.CreateDelegate (
1120 this.GetType ().GetMethod ("Banga"));
1124 public void DynamicInvokeClosedStatic ()
1126 var d1 = Delegate.CreateDelegate (typeof(Func<int>), null, typeof(DelegateTest).GetMethod ("DynamicInvokeClosedStaticDelegate_CB"));
1127 Assert.AreEqual (1, d1.DynamicInvoke (), "#1");
1129 var d2 = Delegate.CreateDelegate (typeof(Func<int>), "arg", typeof(DelegateTest).GetMethod ("DynamicInvokeClosedStaticDelegate_CB"));
1130 Assert.AreEqual (2, d2.DynamicInvoke (), "#2");
1133 public static int DynamicInvokeClosedStaticDelegate_CB (string instance)
1147 public void DynamicInvokeOpenInstanceDelegate ()
1149 var d1 = Delegate.CreateDelegate (typeof (Func<DelegateTest, int>), typeof(DelegateTest).GetMethod ("DynamicInvokeOpenInstanceDelegate_CB"));
1150 Assert.AreEqual (5, d1.DynamicInvoke (new DelegateTest ()), "#1");
1152 var d3 = (Func<DelegateTest, int>) d1;
1153 Assert.AreEqual (5, d3 (null), "#2");
1156 public int DynamicInvokeOpenInstanceDelegate_CB ()
1162 public void DynamicInvoke_InvalidArguments ()
1164 Delegate d = new Func<int, int> (TestMethod);
1167 d.DynamicInvoke (null);
1169 } catch (TargetParameterCountException) {
1173 d.DynamicInvoke (new object [0]);
1175 } catch (TargetParameterCountException) {
1179 public static int TestMethod (int i)
1181 throw new NotSupportedException ();
1184 public static void CreateDelegateOfStaticMethodBoundToNull_Helper (object[] args) {}
1187 public void CreateDelegateOfStaticMethodBoundToNull ()
1189 Type t = typeof (Action);
1190 MethodInfo m = typeof (DelegateTest).GetMethod ("CreateDelegateOfStaticMethodBoundToNull_Helper");
1191 object firstArg = null;
1194 Delegate.CreateDelegate (t, m) ;
1196 } catch (ArgumentException) { }
1199 Delegate.CreateDelegate(t, m, true);
1201 } catch (ArgumentException) { }
1204 Delegate.CreateDelegate(t, m, false);
1205 } catch (ArgumentException) { Assert.Fail ("#3"); }
1208 Delegate.CreateDelegate(t, null, m);
1209 } catch (ArgumentException) { Assert.Fail ("#4"); }
1212 Delegate.CreateDelegate(t, null, m, true);
1213 } catch (ArgumentException) { Assert.Fail ("#5"); }
1216 Delegate.CreateDelegate(t, null, m, false);
1217 } catch (ArgumentException) { Assert.Fail ("#6"); }
1221 public void GetHashCode_Constant () {
1222 Action del = delegate {
1224 int hc1 = del.GetHashCode ();
1226 int hc2 = del.GetHashCode ();
1227 Assert.AreEqual (hc1, hc2);
1230 public interface CreateDelegateIFoo {
1234 public abstract class CreateDelegateFoo {
1235 public abstract int Test ();
1238 public class CreateDelegateMid : CreateDelegateFoo {
1239 public override int Test () {
1244 public class CreateDelegateBar : CreateDelegateMid, CreateDelegateIFoo {
1245 public override int Test () {
1249 public int Test2 () {
1254 delegate int IntNoArgs ();
1257 public void CreateDelegateWithAbstractMethods ()
1259 var f = new CreateDelegateBar ();
1260 var m = typeof (CreateDelegateFoo).GetMethod ("Test");
1261 var m2 = typeof (CreateDelegateMid).GetMethod ("Test");
1262 var m3 = typeof (CreateDelegateIFoo).GetMethod ("Test2");
1264 IntNoArgs a1 = (IntNoArgs)Delegate.CreateDelegate (typeof (IntNoArgs), f, m);
1265 IntNoArgs a2 = (IntNoArgs)Delegate.CreateDelegate (typeof (IntNoArgs), f, m2);
1266 IntNoArgs a3 = (IntNoArgs)Delegate.CreateDelegate (typeof (IntNoArgs), f, m3);
1268 Assert.AreEqual (2, a1 (), "#1");
1269 Assert.AreEqual (2, a2 (), "#2");
1270 Assert.AreEqual (3, a3 (), "#3");
1274 delegate string FooDelegate (Iface iface, string s);
1276 delegate string FooDelegate2 (B b, string s);
1278 public interface Iface
1280 string retarg (string s);
1284 public void CreateDelegateWithLdFtnAndAbstractMethod ()
1286 AssemblyName assemblyName = new AssemblyName ();
1287 assemblyName.Name = "customMod";
1288 assemblyName.Version = new Version (1, 2, 3, 4);
1290 AssemblyBuilder assembly
1291 = Thread.GetDomain ().DefineDynamicAssembly (
1292 assemblyName, AssemblyBuilderAccess.RunAndSave);
1294 ModuleBuilder module = assembly.DefineDynamicModule ("res", "res.dll");
1296 TypeBuilder tb = module.DefineType ("Test2", TypeAttributes.Public, typeof (object));
1300 tb.DefineMethod ("test", MethodAttributes.Public | MethodAttributes.Static,
1301 typeof (int), null);
1302 ILGenerator il = mb.GetILGenerator ();
1304 il.Emit (OpCodes.Newobj, typeof (CreateDelegateBar).GetConstructor (new Type [] { }));
1305 il.Emit (OpCodes.Ldftn, typeof (CreateDelegateIFoo).GetMethod ("Test2"));
1306 il.Emit (OpCodes.Newobj, typeof (IntNoArgs).GetConstructor (new Type [] { typeof (object), typeof (IntPtr) }));
1307 il.Emit (OpCodes.Call, typeof (IntNoArgs).GetMethod ("Invoke"));
1308 il.Emit (OpCodes.Ret);
1311 Type t = tb.CreateType ();
1313 Object obj = Activator.CreateInstance (t, new object [0] { });
1315 int a = (int) t.GetMethod ("test").Invoke (obj, null);
1316 Assert.AreEqual (3, a, "#1");
1319 public static int MethodWithIntParam (int x) {
1324 [Category("NotWorking")]
1325 public void CantBindValueTypeToFirstArg () {
1327 Delegate.CreateDelegate (typeof (Delegate695978_2), 10, typeof (DelegateTest).GetMethod ("MethodWithIntParam"));
1328 Assert.Fail ("create delegate must fail");
1329 } catch (ArgumentException) {}
1332 struct Struct695978 {
1334 public int test() { return value + 10; }
1335 public static int test2 (ref Struct695978 foo) { return foo.value + 20; }
1338 delegate int Delegate695978_1 (ref Struct695978 _this);
1339 delegate int Delegate695978_2 ();
1340 delegate int Delegate695978_3 (Struct695978 _this);
1342 [Test] //tests for #695978
1343 [Category ("NotWorking")]
1344 public void DelegateWithValueTypeArguments ()
1346 Struct695978 es = new Struct695978 ();
1349 var ar1 = (Delegate695978_1)Delegate.CreateDelegate(typeof (Delegate695978_1), typeof (Struct695978).GetMethod("test"));
1350 Assert.IsNotNull (ar1);
1351 Assert.AreEqual (110, ar1 (ref es));
1353 var ar2 = (Delegate695978_2)Delegate.CreateDelegate(typeof (Delegate695978_2), null, typeof (Struct695978).GetMethod("test"));
1354 Assert.IsNotNull (ar2);
1355 Assert.AreEqual (110, ar2 ());
1357 ar1 = (Delegate695978_1) Delegate.CreateDelegate(typeof (Delegate695978_1), typeof (Struct695978).GetMethod("test2"));
1358 Assert.IsNotNull (ar1);
1359 Assert.AreEqual (120, ar1 (ref es));
1362 Delegate.CreateDelegate(typeof (Delegate695978_2), null, typeof (Struct695978).GetMethod("test2"));
1364 } catch (ArgumentException) {}
1367 ar2 = (Delegate695978_2) Delegate.CreateDelegate(typeof (Delegate695978_2), new Struct695978 (), typeof (Struct695978).GetMethod("test"));
1368 Assert.IsNotNull (ar2);
1369 Assert.AreEqual (120, ar2 ());
1372 Delegate.CreateDelegate(typeof (Delegate695978_2), new Struct695978 (), typeof (Struct695978).GetMethod("test2"));
1374 } catch (ArgumentException) {}
1377 Delegate.CreateDelegate(typeof (Delegate695978_3), typeof (Struct695978).GetMethod("test"));
1379 } catch (ArgumentException) {}
1383 public void EnumBaseTypeConversion () {
1384 Func<int, int, bool> dm = Int32D2;
1386 Delegate.CreateDelegate(typeof (Func<StringComparison, StringComparison, bool>), dm.Method) as
1387 Func<StringComparison, StringComparison, bool>;
1388 Assert.IsTrue (d (0, 0));
1392 public static void DynInvokeWithClosedFirstArg (object a, object b)
1397 public void DynamicInvokeClosedOverNullDelegate () {
1398 var dm = new DynamicMethod ("test", typeof (Delegate), null);
1399 var il = dm.GetILGenerator ();
1400 il.Emit (OpCodes.Ldnull);
1401 il.Emit (OpCodes.Ldftn, GetType ().GetMethod ("DynInvokeWithClosedFirstArg"));
1402 il.Emit (OpCodes.Newobj, typeof (Action<object>).GetConstructors ()[0]);
1403 il.Emit (OpCodes.Ret);
1405 var f = (Func <object>) dm.CreateDelegate (typeof (Func <object>));
1406 Action<object> ac = (Action<object>)f();
1407 ac.DynamicInvoke (new object[] { "oi" });
1408 ac.DynamicInvoke (new object[] { null });
1412 public void DynamicInvokeFirstArgBoundDelegate () {
1413 var dm = new DynamicMethod ("test", typeof (Delegate), null);
1414 var il = dm.GetILGenerator ();
1415 il.Emit (OpCodes.Ldstr, "test");
1416 il.Emit (OpCodes.Ldftn, GetType ().GetMethod ("DynInvokeWithClosedFirstArg"));
1417 il.Emit (OpCodes.Newobj, typeof (Action<object>).GetConstructors ()[0]);
1418 il.Emit (OpCodes.Ret);
1420 var f = (Func <object>) dm.CreateDelegate (typeof (Func <object>));
1421 Action<object> ac = (Action<object>)f();
1422 ac.DynamicInvoke (new object[] { "oi" });
1423 ac.DynamicInvoke (new object[] { null });
1427 static bool Int32D2 (int x, int y)
1429 return (x & y) == y;
1434 public virtual string retarg3 (string s) {
1438 static int Run (C x)
1443 public static void DoRun (C x)
1447 public static int StartRun (C x, B b)
1457 public void DoExecute (C c)
1461 public int StartExecute (C c, B b)
1467 public class C : B, Iface
1469 public string retarg (string s) {
1473 public string retarg2 (Iface iface, string s) {
1477 public override string retarg3 (string s) {
1481 static void Run (C x)
1485 public new static int DoRun (C x)
1494 public new int DoExecute (C c)
1507 public static void S (C c)
1511 private void PrivateInstance ()
1516 public delegate void D (C c);
1517 public delegate int E (C c);