Add test for missing binding behavior
[mono.git] / mcs / class / corlib / Test / System / DelegateTest.cs
1 // DelegateTest.cs - NUnit Test Cases for the System.Delegate class
2 //
3 // (C) Ximian, Inc.  http://www.ximian.com
4 //
5
6 using System;
7 using System.Reflection;
8 using System.Reflection.Emit;
9 using System.Threading;
10
11 using NUnit.Framework;
12
13 namespace MonoTests.System
14 {
15         [TestFixture]
16         public class DelegateTest
17         {
18 #if NET_2_0
19                 
20                 public class GenericClass<T> {
21                         public void Method<K> (T t, K k) {}
22                 }
23
24                 public delegate void SimpleDelegate(int a, double b);
25
26
27                 [Test] //See bug #372406
28                 public void CreateDelegate1_Method_Private_Instance ()
29                 {
30                         C c = new C ();
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");
35                         D d = (D) dg;
36                         d (c);
37                 }
38
39                 [Test] //Fixes a regression #377324
40                 public void GetMethodFromGenericClass ()
41                 {
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");
48                 }
49 #endif
50
51                 [Test] // CreateDelegate (Type, MethodInfo)
52                 public void CreateDelegate1_Method_Static ()
53                 {
54                         C c = new C ();
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");
59                         D d = (D) dg;
60                         d (c);
61                 }
62
63                 [Test] // CreateDelegate (Type, MethodInfo)
64                 public void CreateDelegate1_Method_Instance ()
65                 {
66                         C c = new C ();
67                         MethodInfo mi = typeof (C).GetMethod ("M");
68 #if NET_2_0
69                         Delegate dg = Delegate.CreateDelegate (typeof (D), mi);
70                         Assert.AreSame (mi, dg.Method, "#1");
71                         Assert.IsNull (dg.Target, "#2");
72                         D d = (D) dg;
73                         d (c);
74 #else
75                         try {
76                                 Delegate.CreateDelegate (typeof (D), mi);
77                                 Assert.Fail ("#1");
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");
85                         }
86 #endif
87                 }
88
89                 [Test] // CreateDelegate (Type, MethodInfo)
90                 public void CreateDelegate1_Method_Null ()
91                 {
92                         try {
93                                 Delegate.CreateDelegate (typeof (D), (MethodInfo) null);
94                                 Assert.Fail ("#1");
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");
101                         }
102                 }
103
104                 [Test] // CreateDelegate (Type, MethodInfo)
105                 public void CreateDelegate1_Type_Null ()
106                 {
107                         MethodInfo mi = typeof (C).GetMethod ("S");
108                         try {
109                                 Delegate.CreateDelegate ((Type) null, mi);
110                                 Assert.Fail ("#1");
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");
117                         }
118                 }
119
120                 [Test] // CreateDelegate (Type, Object, String)
121                 public void CreateDelegate2 ()
122                 {
123                         E e;
124
125                         e = (E) Delegate.CreateDelegate (typeof (E), new B (), "Execute");
126                         Assert.IsNotNull (e, "#A1");
127                         Assert.AreEqual (4, e (new C ()), "#A2");
128
129                         e = (E) Delegate.CreateDelegate (typeof (E), new C (), "Execute");
130                         Assert.IsNotNull (e, "#B1");
131                         Assert.AreEqual (4, e (new C ()), "#D2");
132
133                         e = (E) Delegate.CreateDelegate (typeof (E), new C (), "DoExecute");
134                         Assert.IsNotNull (e, "#C1");
135                         Assert.AreEqual (102, e (new C ()), "#C2");
136                 }
137
138                 [Test] // CreateDelegate (Type, Object, String)
139                 public void CreateDelegate2_Method_ArgumentsMismatch ()
140                 {
141                         try {
142                                 Delegate.CreateDelegate (typeof (E), new B (),
143                                         "StartExecute");
144                                 Assert.Fail ("#1");
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");
151                         }
152                 }
153
154                 [Test] // CreateDelegate (Type, Object, String)
155                 public void CreateDelegate2_Method_CaseMismatch ()
156                 {
157                         try {
158                                 Delegate.CreateDelegate (typeof (E), new B (), "ExecutE");
159                                 Assert.Fail ("#1");
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");
166                         }
167                 }
168
169                 [Test] // CreateDelegate (Type, Object, String)
170                 public void CreateDelegate2_Method_DoesNotExist ()
171                 {
172                         try {
173                                 Delegate.CreateDelegate (typeof (E), new B (),
174                                         "DoesNotExist");
175                                 Assert.Fail ("#1");
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");
182                         }
183                 }
184
185                 [Test] // CreateDelegate (Type, Object, String)
186                 public void CreateDelegate2_Method_Null ()
187                 {
188                         C c = new C ();
189                         try {
190                                 Delegate.CreateDelegate (typeof (D), c, (string) null);
191                                 Assert.Fail ("#1");
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");
198                         }
199                 }
200
201                 [Test] // CreateDelegate (Type, Object, String)
202                 public void CreateDelegate2_Method_ReturnTypeMismatch ()
203                 {
204                         try {
205                                 Delegate.CreateDelegate (typeof (E), new B (),
206                                         "DoExecute");
207                                 Assert.Fail ("#1");
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");
214                         }
215                 }
216
217                 [Test] // CreateDelegate (Type, Object, String)
218                 public void CreateDelegate2_Method_Static ()
219                 {
220                         try {
221                                 Delegate.CreateDelegate (typeof (E), new B (), "Run");
222                                 Assert.Fail ("#1");
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");
229                         }
230                 }
231
232                 [Test] // CreateDelegate (Type, Object, String)
233                 public void CreateDelegate2_Target_Null ()
234                 {
235                         try {
236                                 Delegate.CreateDelegate (typeof (D),null, "N");
237                                 Assert.Fail ("#1");
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");
244                         }
245                 }
246
247                 [Test] // CreateDelegate (Type, Object, String)
248                 public void CreateDelegate2_Type_Null ()
249                 {
250                         C c = new C ();
251                         try {
252                                 Delegate.CreateDelegate ((Type) null, c, "N");
253                                 Assert.Fail ("#1");
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");
260                         }
261                 }
262
263                 [Test] // CreateDelegate (Type, Type, String)
264                 public void CreateDelegate3 ()
265                 {
266                         E e;
267
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");
272
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");
277
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");
282                 }
283
284                 [Test] // CreateDelegate (Type, Type, String)
285                 public void CreateDelegate3_Method_ArgumentsMismatch ()
286                 {
287                         try {
288                                 Delegate.CreateDelegate (typeof (E), typeof (B),
289                                         "StartRun");
290                                 Assert.Fail ("#1");
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");
297                         }
298                 }
299
300                 [Test] // CreateDelegate (Type, Type, String)
301                 public void CreateDelegate3_Method_CaseMismatch ()
302                 {
303                         try {
304                                 Delegate.CreateDelegate (typeof (E), typeof (B), "RuN");
305                                 Assert.Fail ("#1");
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");
312                         }
313                 }
314
315                 [Test] // CreateDelegate (Type, Type, String)
316                 public void CreateDelegate3_Method_DoesNotExist ()
317                 {
318                         try {
319                                 Delegate.CreateDelegate (typeof (E), typeof (B),
320                                         "DoesNotExist");
321                                 Assert.Fail ("#1");
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");
328                         }
329                 }
330
331                 [Test] // CreateDelegate (Type, Type, String)
332                 public void CreateDelegate3_Method_Instance ()
333                 {
334                         try {
335                                 Delegate.CreateDelegate (typeof (E), typeof (B), "Execute");
336                                 Assert.Fail ("#1");
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");
343                         }
344                 }
345
346                 [Test] // CreateDelegate (Type, Type, String)
347                 public void CreateDelegate3_Method_Null ()
348                 {
349                         try {
350                                 Delegate.CreateDelegate (typeof (D), typeof (C), (string) null);
351                                 Assert.Fail ("#1");
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");
358                         }
359                 }
360
361                 [Test] // CreateDelegate (Type, Type, String)
362                 public void CreateDelegate3_Method_ReturnTypeMismatch ()
363                 {
364                         try {
365                                 Delegate.CreateDelegate (typeof (E), typeof (B),
366                                         "DoRun");
367                                 Assert.Fail ("#1");
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");
374                         }
375                 }
376
377                 [Test] // CreateDelegate (Type, Type, String)
378                 public void CreateDelegate3_Target_Null ()
379                 {
380                         try {
381                                 Delegate.CreateDelegate (typeof (D), (Type) null, "S");
382                                 Assert.Fail ("#1");
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");
389                         }
390                 }
391
392                 [Test] // CreateDelegate (Type, Type, String)
393                 public void CreateDelegate3_Type_Null ()
394                 {
395                         try {
396                                 Delegate.CreateDelegate ((Type) null, typeof (C), "S");
397                                 Assert.Fail ("#1");
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");
404                         }
405                 }
406
407                 [Test] // CreateDelegate (Type, Object, String, Boolean)
408                 public void CreateDelegate4 ()
409                 {
410                         E e;
411
412                         B b = new B ();
413
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");
418
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");
423
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");
428
429                         C c = new C ();
430
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");
435
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");
440
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");
445
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");
450                 }
451
452                 [Test] // CreateDelegate (Type, Object, String, Boolean)
453                 public void CreateDelegate4_Method_ArgumentsMismatch ()
454                 {
455                         try {
456                                 Delegate.CreateDelegate (typeof (E), new B (),
457                                         "StartExecute", false);
458                                 Assert.Fail ("#1");
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");
465                         }
466                 }
467
468                 [Test] // CreateDelegate (Type, Object, String, Boolean)
469                 public void CreateDelegate4_Method_CaseMismatch ()
470                 {
471                         // instance method, case mismatch, do not igore case
472                         try {
473                                 Delegate.CreateDelegate (typeof (E), new B (),
474                                         "ExecutE", false);
475                                 Assert.Fail ("#1");
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");
482                         }
483                 }
484
485                 [Test] // CreateDelegate (Type, Object, String, Boolean)
486                 public void CreateDelegate4_Method_DoesNotExist ()
487                 {
488                         try {
489                                 Delegate.CreateDelegate (typeof (E), new B (),
490                                         "DoesNotExist", false);
491                                 Assert.Fail ("#1");
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");
498                         }
499                 }
500
501                 [Test] // CreateDelegate (Type, Object, String, Boolean)
502                 public void CreateDelegate4_Method_Null ()
503                 {
504                         try {
505                                 Delegate.CreateDelegate (typeof (D), new C (),
506                                         (string) null, true);
507                                 Assert.Fail ("#1");
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");
514                         }
515                 }
516
517                 [Test] // CreateDelegate (Type, Object, String, Boolean)
518                 public void CreateDelegate4_Method_ReturnTypeMismatch ()
519                 {
520                         try {
521                                 Delegate.CreateDelegate (typeof (E), new B (),
522                                         "DoExecute", false);
523                                 Assert.Fail ("#1");
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");
530                         }
531                 }
532
533                 [Test] // CreateDelegate (Type, Object, String, Boolean)
534                 public void CreateDelegate4_Method_Static ()
535                 {
536                         try {
537                                 Delegate.CreateDelegate (typeof (E), new B (), "Run", true);
538                                 Assert.Fail ("#1");
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");
545                         }
546                 }
547
548                 [Test] // CreateDelegate (Type, Object, String, Boolean)
549                 public void CreateDelegate4_Target_Null ()
550                 {
551                         try {
552                                 Delegate.CreateDelegate (typeof (D), null, "N", true);
553                                 Assert.Fail ("#1");
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");
560                         }
561                 }
562
563                 [Test] // CreateDelegate (Type, Object, String, Boolean)
564                 public void CreateDelegate4_Type_Null ()
565                 {
566                         C c = new C ();
567                         try {
568                                 Delegate.CreateDelegate ((Type) null, c, "N", true);
569                                 Assert.Fail ("#1");
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");
576                         }
577                 }
578
579 #if NET_2_0
580                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
581                 public void CreateDelegate9 ()
582                 {
583                         E e;
584
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");
590
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");
596
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");
602
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");
608
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");
614
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");
620
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");
626
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");
632
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");
638
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");
644
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");
650
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");
656                 }
657
658                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
659                 public void CreateDelegate9_Method_ArgumentsMismatch ()
660                 {
661                         // throw bind failure
662                         try {
663                                 Delegate.CreateDelegate (typeof (E), new B (),
664                                         "StartExecute", false, true);
665                                 Assert.Fail ("#A1");
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");
672                         }
673
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");
678                 }
679
680                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
681                 public void CreateDelegate9_Method_CaseMismatch ()
682                 {
683                         E e;
684
685                         // do not ignore case, throw bind failure
686                         try {
687                                 Delegate.CreateDelegate (typeof (E), new B (),
688                                         "ExecutE", false, true);
689                                 Assert.Fail ("#A1");
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");
696                         }
697
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");
702
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");
708
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");
714                 }
715
716                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
717                 public void CreateDelegate9_Method_DoesNotExist ()
718                 {
719                         // throw bind failure
720                         try {
721                                 Delegate.CreateDelegate (typeof (E), new B (),
722                                         "DoesNotExist", false, true);
723                                 Assert.Fail ("#A1");
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");
730                         }
731
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");
736                 }
737
738                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
739                 public void CreateDelegate9_Method_Null ()
740                 {
741                         try {
742                                 Delegate.CreateDelegate (typeof (E), new B (),
743                                         (string) null, false, false);
744                                 Assert.Fail ("#1");
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");
751                         }
752                 }
753
754                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
755                 public void CreateDelegate9_Method_ReturnTypeMismatch ()
756                 {
757                         // throw bind failure
758                         try {
759                                 Delegate.CreateDelegate (typeof (E), new B (),
760                                         "DoExecute", false, true);
761                                 Assert.Fail ("#A1");
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");
768                         }
769
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");
774                 }
775
776                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
777                 public void CreateDelegate9_Method_Static ()
778                 {
779                         // throw bind failure
780                         try {
781                                 Delegate.CreateDelegate (typeof (E), new B (),
782                                         "Run", true, true);
783                                 Assert.Fail ("#A1");
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");
790                         }
791
792                         // do not throw on bind failure
793                         E e = (E) Delegate.CreateDelegate (typeof (E), new B (),
794                                 "Run", true, false);
795                         Assert.IsNull (e, "#B");
796                 }
797
798                 [Test] // CreateDelegate (Type, Object, String, Boolean, Boolean)
799                 public void CreateDelegate9_Target_Null ()
800                 {
801                         try {
802                                 Delegate.CreateDelegate (typeof (E),(object) null,
803                                         "Execute", true, false);
804                                 Assert.Fail ("#1");
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");
811                         }
812                 }
813
814                 [Test] // CreateDelegate (Type, Object, String, Boolean)
815                 public void CreateDelegate9_Type_Null ()
816                 {
817                         try {
818                                 Delegate.CreateDelegate ((Type) null, new B (),
819                                         "Execute", true, false);
820                                 Assert.Fail ("#1");
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");
827                         }
828                 }
829
830                 class ParentClass
831                 {
832                 }
833
834                 class Subclass : ParentClass
835                 {
836                 }
837
838                 delegate ParentClass CoContraVariantDelegate (Subclass s);
839
840                 static Subclass CoContraVariantMethod (ParentClass s)
841                 {
842                         return null;
843                 }
844
845                 [Test]
846                 [Category ("TargetJvmNotWorking")]
847                 public void CoContraVariance ()
848                 {
849                         CoContraVariantDelegate d = (CoContraVariantDelegate)
850                                 Delegate.CreateDelegate (typeof (CoContraVariantDelegate),
851                                         typeof (DelegateTest).GetMethod ("CoContraVariantMethod",
852                                         BindingFlags.NonPublic|BindingFlags.Static));
853                         d (null);
854                 }
855
856                 static object Box (object o)
857                 {
858                         return o;
859                 }
860
861                 delegate object Boxer ();
862
863                 [Test]
864                 public void BoxingCovariance ()
865                 {
866                         var boxer = (Boxer) Delegate.CreateDelegate (
867                                 typeof (Boxer),
868                                 42,
869                                 GetType ().GetMethod ("Box", BindingFlags.NonPublic | BindingFlags.Static));
870
871                         Assert.IsNotNull (boxer);
872                         Assert.AreEqual (42, boxer ());
873                 }
874
875                 static object Nada (int o)
876                 {
877                         return (int) o;
878                 }
879
880                 delegate int WrongDelegate ();
881
882                 [Test]
883                 [ExpectedException (typeof (ArgumentException))]
884                 public void RemoveDelegateTypeMismatch ()
885                 {
886                         Delegate boxer = new Boxer (() => new object ());
887                         Delegate.Remove (boxer, new WrongDelegate (() => 42));
888                 }
889
890                 [Test]
891                 [ExpectedException (typeof (ArgumentException))]
892                 public void WrongReturnTypeContravariance ()
893                 {
894                         Delegate.CreateDelegate (
895                                 typeof (WrongDelegate),
896                                 42,
897                                 GetType ().GetMethod ("Nada", BindingFlags.NonPublic | BindingFlags.Static));
898                 }
899
900                 static int Identity (int i)
901                 {
902                         return i;
903                 }
904
905                 [Test]
906                 [ExpectedException (typeof (ArgumentException))]
907                 public void WrongReturnTypeContravariance_2 ()
908                 {
909                         Delegate.CreateDelegate (
910                                 typeof (Boxer),
911                                 42,
912                                 GetType ().GetMethod ("Identity", BindingFlags.NonPublic | BindingFlags.Static));
913                 }
914
915                 delegate object CallTarget ();
916
917                 class Closure {}
918
919                 static object Target (Closure c)
920                 {
921                         return c;
922                 }
923
924                 [Test]
925                 public void NullFirstArgumentOnStaticMethod ()
926                 {
927                         CallTarget call = (CallTarget) Delegate.CreateDelegate (
928                                 typeof (CallTarget),
929                                 null,
930                                 GetType ().GetMethod ("Target", BindingFlags.NonPublic | BindingFlags.Static));
931
932                         Assert.IsNotNull (call);
933                         Assert.IsNull (call.Target);
934                         Assert.IsNull (call ());
935                 }
936
937                 [Test]
938                 public void Virtual ()
939                 {
940                         // Delegate with abstract method, no target
941                         FooDelegate del = (FooDelegate)Delegate.CreateDelegate (typeof (FooDelegate), typeof (Iface).GetMethod ("retarg"));
942                         C c = new C ();
943                         Assert.AreEqual ("Hello", del (c, "Hello"));
944
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);
949
950                         Assert.AreEqual ("Hello2", del4 (c, "Hello"));
951
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"));
955                 }
956
957                 int int_field;
958
959                 delegate int Del1 (DelegateTest dt, int i);
960
961                 public int method1 (int i) {
962                         return int_field + i;
963                 }
964
965                 [Test]
966                 public void NullTarget_Instance ()
967                 {
968                         Del1 d = (Del1)Delegate.CreateDelegate (typeof (Del1), null, typeof (DelegateTest).GetMethod ("method1"));
969
970                         DelegateTest dt = new DelegateTest ();
971                         dt.int_field = 5;
972
973                         Assert.AreEqual (10, d (dt, 5));
974                 }
975
976                 delegate int Del2 (int i);
977
978                 public static int method2 (int i) {
979                         return i + 5;
980                 }
981
982                 [Test]
983                 public void NullTarget_Static ()
984                 {
985                         Del2 d = (Del2)Delegate.CreateDelegate (typeof (Del2), null, typeof (DelegateTest).GetMethod ("method2"));
986
987                         Assert.AreEqual (10, d (5));
988                 }
989
990                 delegate int Del3 (int i);
991
992                 public int method3 (int i) {
993                         return int_field + 5;
994                 }
995
996                 [Test]
997                 public void HasTarget_Instance ()
998                 {
999                         DelegateTest dt = new DelegateTest ();
1000                         dt.int_field = 5;
1001
1002                         Del3 d = (Del3)Delegate.CreateDelegate (typeof (Del3), dt, typeof (DelegateTest).GetMethod ("method3"));
1003
1004                         Assert.AreEqual (10, d (5));
1005                 }
1006
1007                 delegate int Del4 (int i);
1008
1009                 public static int method4 (string s, int i) {
1010                         return Int32.Parse (s) + 5;
1011                 }
1012
1013                 [Test]
1014                 public void HasTarget_Static ()
1015                 {
1016                         Del4 d = (Del4)Delegate.CreateDelegate (typeof (Del4), "5", typeof (DelegateTest).GetMethod ("method4"));
1017
1018                         Assert.AreEqual (10, d (5));
1019                 }
1020
1021                 public static long? StaticMethodToBeClosedOverNull (object o, long? bar)
1022                 {
1023                         Console.WriteLine ("o: {0}", o);
1024                         return 5;
1025                 }
1026
1027                 [Test] // #617161
1028                 public void ClosedOverNullReferenceStaticMethod ()
1029                 {
1030                         var del = (Func<long?,long?>) Delegate.CreateDelegate (
1031                                 typeof (Func<long?,long?>),
1032                                 null as object,
1033                                 this.GetType ().GetMethod ("StaticMethodToBeClosedOverNull"));
1034
1035                         Assert.IsNull (del.Target);
1036
1037                         Assert.AreEqual ((long?) 5, del (5));
1038                 }
1039
1040                 public void InstanceMethodToBeClosedOverNull ()
1041                 {
1042                 }
1043
1044                 public void InstanceMethodIntToBeClosedOverNull (int i)
1045                 {
1046                 }
1047
1048                 [Test] // #475962
1049                 public void ClosedOverNullReferenceInstanceMethod ()
1050                 {
1051                         var action = (Action) Delegate.CreateDelegate (
1052                                 typeof (Action),
1053                                 null as object,
1054                                 this.GetType ().GetMethod ("InstanceMethodToBeClosedOverNull"));
1055
1056                         Assert.IsNull (action.Target);
1057
1058                         action ();
1059
1060                         var action_int = (Action<int>) Delegate.CreateDelegate (
1061                                 typeof (Action<int>),
1062                                 null as object,
1063                                 this.GetType ().GetMethod ("InstanceMethodIntToBeClosedOverNull"));
1064
1065                         Assert.IsNull (action.Target);
1066
1067                         action_int (42);
1068                 }
1069
1070                 class Foo {
1071
1072                         public void Bar ()
1073                         {
1074                         }
1075                 }
1076
1077                 Foo foo;
1078                 event Action bar_handler;
1079
1080                 [Test]
1081                 [ExpectedException (typeof (ArgumentException))] // #635349, #605936
1082                 public void NewDelegateClosedOverNullReferenceInstanceMethod ()
1083                 {
1084                         bar_handler += foo.Bar;
1085                 }
1086
1087                 public void Banga ()
1088                 {
1089                 }
1090
1091                 [Test]
1092                 [ExpectedException (typeof (ArgumentException))]
1093                 public void CreateDelegateOpenOnly ()
1094                 {
1095                         Delegate.CreateDelegate (
1096                                 typeof (Action),
1097                                 this.GetType ().GetMethod ("Banga"));
1098                 }
1099
1100                 [Test] // #664205
1101                 public void DynamicInvokeNullTarget ()
1102                 {
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);
1107
1108                         var @delegate = method.CreateDelegate (typeof (Func<int>), null);
1109
1110                         Assert.AreEqual (42, (int) @delegate.DynamicInvoke ());
1111                 }
1112
1113 #endif
1114                 public static void CreateDelegateOfStaticMethodBoundToNull_Helper (object[] args) {}
1115
1116                 [Test]
1117                 public void CreateDelegateOfStaticMethodBoundToNull ()
1118                 {
1119                         Type t = typeof (Action);
1120                         MethodInfo m = typeof (DelegateTest).GetMethod ("CreateDelegateOfStaticMethodBoundToNull_Helper");
1121                         object firstArg = null;
1122         
1123                         try {
1124                                 Delegate.CreateDelegate (t, m) ;
1125                                 Assert.Fail ("#1");
1126                         } catch (ArgumentException) {  }
1127         
1128                         try {
1129                                 Delegate.CreateDelegate(t, m, true);
1130                                 Assert.Fail ("#2");
1131                         } catch (ArgumentException) {  }
1132         
1133                         try {
1134                                 Delegate.CreateDelegate(t, m, false);
1135                         } catch (ArgumentException) { Assert.Fail ("#3"); }
1136         
1137                         try {
1138                                 Delegate.CreateDelegate(t, null, m);
1139                         } catch (ArgumentException) { Assert.Fail ("#4"); }
1140         
1141                         try {
1142                                 Delegate.CreateDelegate(t, null, m, true);
1143                         } catch (ArgumentException) { Assert.Fail ("#5");  }
1144         
1145                         try {
1146                                 Delegate.CreateDelegate(t, null, m, false);
1147                         } catch (ArgumentException) { Assert.Fail ("#6"); }
1148                 }
1149
1150                 [Test]
1151                 public void GetHashCode_Constant () {
1152                         Action del = delegate {
1153                         };
1154                         int hc1 = del.GetHashCode ();
1155                         del ();
1156                         int hc2 = del.GetHashCode ();
1157                         Assert.AreEqual (hc1, hc2);
1158                 }
1159
1160                 public interface CreateDelegateIFoo {
1161                         int Test2 ();
1162                 }
1163                 
1164                 public abstract class CreateDelegateFoo {
1165                         public abstract int Test ();
1166                 }
1167                 
1168                 public class CreateDelegateMid : CreateDelegateFoo {
1169                         public override int Test () {
1170                                 return 1;
1171                         }
1172                 }
1173                 
1174                 public class CreateDelegateBar : CreateDelegateMid, CreateDelegateIFoo {
1175                         public override int Test () {
1176                                 return 2;
1177                         }
1178                 
1179                         public int Test2 () {
1180                                 return 3;
1181                         }
1182                 }
1183         
1184                 delegate int IntNoArgs ();
1185
1186                 [Test]
1187                 public void CreateDelegateWithAbstractMethods ()
1188                 {
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");
1193         
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);
1197
1198                         Assert.AreEqual (2, a1 (), "#1");
1199                         Assert.AreEqual (2, a2 (), "#2");
1200                         Assert.AreEqual (3, a3 (), "#3");
1201                 }
1202                 
1203
1204                 delegate string FooDelegate (Iface iface, string s);
1205
1206                 delegate string FooDelegate2 (B b, string s);
1207
1208                 public interface Iface
1209                 {
1210                         string retarg (string s);
1211                 }
1212
1213                 [Test]
1214                 public void CreateDelegateWithLdFtnAndAbstractMethod ()
1215                 {
1216                         AssemblyName assemblyName = new AssemblyName ();
1217                         assemblyName.Name = "customMod";
1218                         assemblyName.Version = new Version (1, 2, 3, 4);
1219         
1220                         AssemblyBuilder assembly
1221                                 = Thread.GetDomain ().DefineDynamicAssembly (
1222                                           assemblyName, AssemblyBuilderAccess.RunAndSave);
1223         
1224                         ModuleBuilder module = assembly.DefineDynamicModule ("res", "res.dll");
1225         
1226                         TypeBuilder tb = module.DefineType ("Test2", TypeAttributes.Public, typeof (object));
1227         
1228                         {
1229                                 MethodBuilder mb =
1230                                         tb.DefineMethod ("test", MethodAttributes.Public | MethodAttributes.Static,
1231                                                                          typeof (int), null);
1232                                 ILGenerator il = mb.GetILGenerator ();
1233         
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);
1239                         }
1240         
1241                         Type t = tb.CreateType ();
1242         
1243                         Object obj = Activator.CreateInstance (t, new object [0] { });
1244         
1245                         int a = (int) t.GetMethod ("test").Invoke (obj, null);
1246                         Assert.AreEqual (3, a, "#1");
1247                 }
1248
1249                 public static int MethodWithIntParam (int x) {
1250                         return 10;
1251                 }
1252
1253                 [Test]
1254                 [Category("NotWorking")]
1255                 public void CantBindValueTypeToFirstArg () {
1256                         try {
1257                                 Delegate.CreateDelegate (typeof (Delegate695978_2), 10, typeof (DelegateTest).GetMethod ("MethodWithIntParam"));
1258                                 Assert.Fail ("create delegate must fail");
1259                         } catch (ArgumentException) {}
1260                 }
1261
1262                 struct Struct695978 {
1263                         public int value;
1264                         public int test() { return value + 10; }
1265                         public static int test2 (ref Struct695978 foo) { return foo.value + 20; }
1266                 }
1267
1268                 delegate int Delegate695978_1 (ref Struct695978 _this);
1269                 delegate int Delegate695978_2 ();
1270                 delegate int Delegate695978_3 (Struct695978 _this);
1271
1272                 [Test] //tests for #695978
1273                 public void DelegateWithValueTypeArguments ()
1274                 {
1275                         Struct695978 es = new Struct695978 ();
1276                         es.value = 100;
1277
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");
1281
1282                         var ar2 = (Delegate695978_2)Delegate.CreateDelegate(typeof (Delegate695978_2), null, typeof (Struct695978).GetMethod("test"));
1283                         Assert.IsNotNull (ar2);
1284                         try {
1285                                 ar2 ();
1286                                 Assert.Fail ("#i2");
1287                         } catch (NullReferenceException) {}
1288
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");
1292
1293                         try {
1294                                 Delegate.CreateDelegate(typeof (Delegate695978_2), null, typeof (Struct695978).GetMethod("test2"));
1295                                 Assert.Fail ("#1");
1296                         } catch (ArgumentException) {}
1297
1298
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");
1302
1303                         try {
1304                                 Delegate.CreateDelegate(typeof (Delegate695978_2), new Struct695978 (), typeof (Struct695978).GetMethod("test2"));
1305                                 Assert.Fail ("#2");
1306                         } catch (ArgumentException) {}
1307
1308                         try {
1309                                 Delegate.CreateDelegate(typeof (Delegate695978_3), typeof (Struct695978).GetMethod("test"));
1310                                 Assert.Fail ("#3");
1311                         } catch (ArgumentException) {}
1312                 }
1313
1314                 public class B {
1315
1316                         public virtual string retarg3 (string s) {
1317                                 return s;
1318                         }
1319
1320                         static int Run (C x)
1321                         {
1322                                 return 5;
1323                         }
1324
1325                         public static void DoRun (C x)
1326                         {
1327                         }
1328
1329                         public static int StartRun (C x, B b)
1330                         {
1331                                 return 6;
1332                         }
1333
1334                         int Execute (C c)
1335                         {
1336                                 return 4;
1337                         }
1338
1339                         public void DoExecute (C c)
1340                         {
1341                         }
1342
1343                         public int StartExecute (C c, B b)
1344                         {
1345                                 return 3;
1346                         }
1347                 }
1348
1349                 public class C : B, Iface
1350                 {
1351                         public string retarg (string s) {
1352                                 return s;
1353                         }
1354
1355                         public string retarg2 (Iface iface, string s) {
1356                                 return s + "2";
1357                         }
1358
1359                         public override string retarg3 (string s) {
1360                                 return s + "2";
1361                         }
1362
1363                         static void Run (C x)
1364                         {
1365                         }
1366
1367                         public new static int DoRun (C x)
1368                         {
1369                                 return 107;
1370                         }
1371
1372                         void Execute (C c)
1373                         {
1374                         }
1375
1376                         public new int DoExecute (C c)
1377                         {
1378                                 return 102;
1379                         }
1380
1381                         public void M ()
1382                         {
1383                         }
1384
1385                         public void N (C c)
1386                         {
1387                         }
1388
1389                         public static void S (C c)
1390                         {
1391                         }
1392
1393                         private void PrivateInstance ()
1394                         {
1395                         }
1396                 }
1397
1398                 public delegate void D (C c);
1399                 public delegate int E (C c);
1400         }
1401 }