This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDelegateTests.cs
More file actions
1202 lines (1022 loc) · 41.9 KB
/
Copy pathDelegateTests.cs
File metadata and controls
1202 lines (1022 loc) · 41.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Tests;
using Xunit;
namespace System.Tests
{
public static class TestExtensionMethod
{
public static DelegateTests.TestStruct TestFunc(this DelegateTests.TestClass testparam)
{
return testparam.structField;
}
public static void IncrementX(this DelegateTests.TestSerializableClass t)
{
t.x++;
}
}
public static unsafe class DelegateTests
{
public struct TestStruct
{
public object o1;
public object o2;
}
public class TestClass
{
public TestStruct structField;
}
[Serializable]
public class TestSerializableClass
{
public int x = 1;
}
private static void EmptyFunc() { }
public delegate TestStruct StructReturningDelegate();
[Fact]
public static void ClosedStaticDelegate()
{
TestClass foo = new TestClass();
foo.structField.o1 = new object();
foo.structField.o2 = new object();
StructReturningDelegate testDelegate = foo.TestFunc;
TestStruct returnedStruct = testDelegate();
Assert.Same(foo.structField.o1, returnedStruct.o1);
Assert.Same(foo.structField.o2, returnedStruct.o2);
}
public class A { }
public class B : A { }
public delegate A DynamicInvokeDelegate(A nonRefParam1, B nonRefParam2, ref A refParam, out B outParam);
public static A DynamicInvokeTestFunction(A nonRefParam1, B nonRefParam2, ref A refParam, out B outParam)
{
outParam = (B)refParam;
refParam = nonRefParam2;
return nonRefParam1;
}
[Fact]
public static void DynamicInvoke()
{
A a1 = new A();
A a2 = new A();
B b1 = new B();
B b2 = new B();
DynamicInvokeDelegate testDelegate = DynamicInvokeTestFunction;
// Check that the delegate behaves as expected
A refParam = b2;
B outParam = null;
A returnValue = testDelegate(a1, b1, ref refParam, out outParam);
Assert.Same(returnValue, a1);
Assert.Same(refParam, b1);
Assert.Same(outParam, b2);
// Check dynamic invoke behavior
object[] parameters = new object[] { a1, b1, b2, null };
object retVal = testDelegate.DynamicInvoke(parameters);
Assert.Same(retVal, a1);
Assert.Same(parameters[2], b1);
Assert.Same(parameters[3], b2);
// Check invoke on a delegate that takes no parameters.
Action emptyDelegate = EmptyFunc;
emptyDelegate.DynamicInvoke(new object[] { });
emptyDelegate.DynamicInvoke(null);
}
[Fact]
public static void DynamicInvoke_MissingTypeForDefaultParameter_Succeeds()
{
// Passing Type.Missing with default.
Delegate d = new IntIntDelegateWithDefault(IntIntMethod);
d.DynamicInvoke(7, Type.Missing);
}
[Fact]
public static void DynamicInvoke_MissingTypeForNonDefaultParameter_ThrowsArgumentException()
{
Delegate d = new IntIntDelegate(IntIntMethod);
AssertExtensions.Throws<ArgumentException>("parameters", () => d.DynamicInvoke(7, Type.Missing));
}
[Theory]
[InlineData(new object[] { 7 }, new object[] { 8 })]
[InlineData(new object[] { null }, new object[] { 1 })]
public static void DynamicInvoke_RefValueTypeParameter(object[] args, object[] expected)
{
Delegate d = new RefIntDelegate(RefIntMethod);
d.DynamicInvoke(args);
Assert.Equal(expected, args);
}
[Fact]
public static void DynamicInvoke_NullRefValueTypeParameter_ReturnsValueTypeDefault()
{
Delegate d = new RefValueTypeDelegate(RefValueTypeMethod);
object[] args = new object[] { null };
d.DynamicInvoke(args);
MyStruct s = (MyStruct)(args[0]);
Assert.Equal(7, s.X);
Assert.Equal(8, s.Y);
}
[Fact]
public static void DynamicInvoke_TypeDoesntExactlyMatchRefValueType_ThrowsArgumentException()
{
Delegate d = new RefIntDelegate(RefIntMethod);
AssertExtensions.Throws<ArgumentException>(null, () => d.DynamicInvoke((uint)7));
AssertExtensions.Throws<ArgumentException>(null, () => d.DynamicInvoke(IntEnum.One));
}
[Theory]
[InlineData(7, (short)7)] // uint -> int
[InlineData(7, IntEnum.Seven)] // Enum (int) -> int
[InlineData(7, ShortEnum.Seven)] // Enum (short) -> int
public static void DynamicInvoke_ValuePreservingPrimitiveWidening_Succeeds(object o1, object o2)
{
Delegate d = new IntIntDelegate(IntIntMethod);
d.DynamicInvoke(o1, o2);
}
[Theory]
[InlineData(IntEnum.Seven, 7)]
[InlineData(IntEnum.Seven, (short)7)]
public static void DynamicInvoke_ValuePreservingWideningToEnum_Succeeds(object o1, object o2)
{
Delegate d = new EnumEnumDelegate(EnumEnumMethod);
d.DynamicInvoke(o1, o2);
}
[Fact]
public static void DynamicInvoke_SizePreservingNonVauePreservingConversion_ThrowsArgumentException()
{
Delegate d = new IntIntDelegate(IntIntMethod);
AssertExtensions.Throws<ArgumentException>(null, () => d.DynamicInvoke(7, (uint)7));
AssertExtensions.Throws<ArgumentException>(null, () => d.DynamicInvoke(7, U4.Seven));
}
[Fact]
public static void DynamicInvoke_NullValueType_Succeeds()
{
Delegate d = new ValueTypeDelegate(ValueTypeMethod);
d.DynamicInvoke(new object[] { null });
}
[Fact]
public static void DynamicInvoke_ConvertMatchingTToNullable_Succeeds()
{
Delegate d = new NullableDelegate(NullableMethod);
d.DynamicInvoke(7);
}
[Fact]
public static void DynamicInvoke_ConvertNonMatchingTToNullable_ThrowsArgumentException()
{
Delegate d = new NullableDelegate(NullableMethod);
AssertExtensions.Throws<ArgumentException>(null, () => d.DynamicInvoke((short)7));
AssertExtensions.Throws<ArgumentException>(null, () => d.DynamicInvoke(IntEnum.Seven));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_AllPrimitiveParametersWithMissingValues()
{
object[] parameters = new object[13];
for (int i = 0; i < parameters.Length; i++) { parameters[i] = Type.Missing; }
Assert.Equal(
"True, test, c, 2, -1, -3, 4, -5, 6, -7, 8, 9.1, 11.12",
(string)(new AllPrimitivesWithDefaultValues(AllPrimitivesMethod)).DynamicInvoke(parameters));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_AllPrimitiveParametersWithAllExplicitValues()
{
Assert.Equal(
"False, value, d, 102, -101, -103, 104, -105, 106, -107, 108, 109.1, 111.12",
(string)(new AllPrimitivesWithDefaultValues(AllPrimitivesMethod)).DynamicInvoke(
new object[13]
{
false,
"value",
'd',
(byte)102,
(sbyte)-101,
(short)-103,
(ushort)104,
(int)-105,
(uint)106,
(long)-107,
(ulong)108,
(float)109.1,
(double)111.12
}
));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_AllPrimitiveParametersWithSomeExplicitValues()
{
Assert.Equal(
"False, test, d, 2, -101, -3, 104, -5, 106, -7, 108, 9.1, 111.12",
(string)(new AllPrimitivesWithDefaultValues(AllPrimitivesMethod)).DynamicInvoke(
new object[13]
{
false,
Type.Missing,
'd',
Type.Missing,
(sbyte)-101,
Type.Missing,
(ushort)104,
Type.Missing,
(uint)106,
Type.Missing,
(ulong)108,
Type.Missing,
(double)111.12
}
));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_StringParameterWithMissingValue()
{
Assert.Equal
("test",
(string)(new StringWithDefaultValue(StringMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_StringParameterWithExplicitValue()
{
Assert.Equal(
"value",
(string)(new StringWithDefaultValue(StringMethod)).DynamicInvoke(new object[] { "value" }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_ReferenceTypeParameterWithMissingValue()
{
Assert.Null((new ReferenceWithDefaultValue(ReferenceMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_ReferenceTypeParameterWithExplicitValue()
{
CustomReferenceType referenceInstance = new CustomReferenceType();
Assert.Same(
referenceInstance,
(new ReferenceWithDefaultValue(ReferenceMethod)).DynamicInvoke(new object[] { referenceInstance }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_ValueTypeParameterWithMissingValue()
{
Assert.Equal(
0,
((CustomValueType)(new ValueTypeWithDefaultValue(ValueTypeMethod)).DynamicInvoke(new object[] { Type.Missing })).Id);
}
[Fact]
public static void DynamicInvoke_DefaultParameter_ValueTypeParameterWithExplicitValue()
{
Assert.Equal(
1,
((CustomValueType)(new ValueTypeWithDefaultValue(ValueTypeMethod)).DynamicInvoke(new object[] { new CustomValueType { Id = 1 } })).Id);
}
[Fact]
public static void DynamicInvoke_DefaultParameter_DateTimeParameterWithMissingValue()
{
Assert.Equal(
new DateTime(42),
(DateTime)(new DateTimeWithDefaultValueAttribute(DateTimeMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_DateTimeParameterWithExplicitValue()
{
Assert.Equal(
new DateTime(43),
(DateTime)(new DateTimeWithDefaultValueAttribute(DateTimeMethod)).DynamicInvoke(new object[] { new DateTime(43) }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_DecimalParameterWithAttributeAndMissingValue()
{
Assert.Equal(
new decimal(4, 3, 2, true, 1),
(decimal)(new DecimalWithDefaultValueAttribute(DecimalMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_DecimalParameterWithAttributeAndExplicitValue()
{
Assert.Equal(
new decimal(12, 13, 14, true, 1),
(decimal)(new DecimalWithDefaultValueAttribute(DecimalMethod)).DynamicInvoke(new object[] { new decimal(12, 13, 14, true, 1) }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_DecimalParameterWithMissingValue()
{
Assert.Equal(
3.14m,
(decimal)(new DecimalWithDefaultValue(DecimalMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_DecimalParameterWithExplicitValue()
{
Assert.Equal(
103.14m,
(decimal)(new DecimalWithDefaultValue(DecimalMethod)).DynamicInvoke(new object[] { 103.14m }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_NullableIntWithMissingValue()
{
Assert.Null((int?)(new NullableIntWithDefaultValue(NullableIntMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_NullableIntWithExplicitValue()
{
Assert.Equal(
(int?)42,
(int?)(new NullableIntWithDefaultValue(NullableIntMethod)).DynamicInvoke(new object[] { (int?)42 }));
}
[Fact]
public static void DynamicInvoke_DefaultParameter_EnumParameterWithMissingValue()
{
Assert.Equal(
IntEnum.Seven,
(IntEnum)(new EnumWithDefaultValue(EnumMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_OptionalParameter_WithExplicitValue()
{
Assert.Equal(
"value",
(new OptionalObjectParameter(ObjectMethod)).DynamicInvoke(new object[] { "value" }));
}
[Fact]
public static void DynamicInvoke_OptionalParameter_WithMissingValue()
{
Assert.Equal(
Type.Missing,
(new OptionalObjectParameter(ObjectMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_OptionalParameterUnassingableFromMissing_WithMissingValue()
{
AssertExtensions.Throws<ArgumentException>(null, () => (new OptionalStringParameter(StringMethod)).DynamicInvoke(new object[] { Type.Missing }));
}
[Fact]
public static void DynamicInvoke_ParameterSpecification_ArrayOfStrings()
{
Assert.Equal(
"value",
(new StringParameter(StringMethod)).DynamicInvoke(new string[] { "value" }));
}
[Fact]
public static void DynamicInvoke_ParameterSpecification_ArrayOfMissing()
{
Assert.Same(
Missing.Value,
(new OptionalObjectParameter(ObjectMethod)).DynamicInvoke(new Missing[] { Missing.Value }));
}
private static void IntIntMethod(int expected, int actual)
{
Assert.Equal(expected, actual);
}
private delegate void IntIntDelegate(int expected, int actual);
private delegate void IntIntDelegateWithDefault(int expected, int actual = 7);
private static void RefIntMethod(ref int i) => i++;
private delegate void RefIntDelegate(ref int i);
private struct MyStruct
{
public int X;
public int Y;
}
private static void RefValueTypeMethod(ref MyStruct s)
{
s.X += 7;
s.Y += 8;
}
private delegate void RefValueTypeDelegate(ref MyStruct s);
private static void EnumEnumMethod(IntEnum expected, IntEnum actual)
{
Assert.Equal(expected, actual);
}
private delegate void EnumEnumDelegate(IntEnum expected, IntEnum actual);
private static void ValueTypeMethod(MyStruct s)
{
Assert.Equal(0, s.X);
Assert.Equal(0, s.Y);
}
private delegate void ValueTypeDelegate(MyStruct s);
private static void NullableMethod(int? n)
{
Assert.True(n.HasValue);
Assert.Equal(7, n.Value);
}
private delegate void NullableDelegate(int? s);
public enum ShortEnum : short
{
One = 1,
Seven = 7,
}
public enum IntEnum : int
{
One = 1,
Seven = 7,
}
public enum U4 : uint
{
One = 1,
Seven = 7,
}
private delegate string AllPrimitivesWithDefaultValues(
bool boolean = true,
string str = "test",
char character = 'c',
byte unsignedbyte = 2,
sbyte signedbyte = -1,
short int16 = -3,
ushort uint16 = 4,
int int32 = -5,
uint uint32 = 6,
long int64 = -7,
ulong uint64 = 8,
float single = (float)9.1,
double dbl = 11.12);
private static string AllPrimitivesMethod(
bool boolean,
string str,
char character,
byte unsignedbyte,
sbyte signedbyte,
short int16,
ushort uint16,
int int32,
uint uint32,
long int64,
ulong uint64,
float single,
double dbl)
{
return FormattableString.Invariant($"{boolean}, {str}, {character}, {unsignedbyte}, {signedbyte}, {int16}, {uint16}, {int32}, {uint32}, {int64}, {uint64}, {single}, {dbl}");
}
private delegate string StringParameter(string parameter);
private delegate string StringWithDefaultValue(string parameter = "test");
private static string StringMethod(string parameter)
{
return parameter;
}
private class CustomReferenceType { };
private delegate CustomReferenceType ReferenceWithDefaultValue(CustomReferenceType parameter = null);
private static CustomReferenceType ReferenceMethod(CustomReferenceType parameter)
{
return parameter;
}
private struct CustomValueType { public int Id; };
private delegate CustomValueType ValueTypeWithDefaultValue(CustomValueType parameter = default(CustomValueType));
private static CustomValueType ValueTypeMethod(CustomValueType parameter)
{
return parameter;
}
private delegate DateTime DateTimeWithDefaultValueAttribute([DateTimeConstant(42)] DateTime parameter);
private static DateTime DateTimeMethod(DateTime parameter)
{
return parameter;
}
private delegate decimal DecimalWithDefaultValueAttribute([DecimalConstant(1, 1, 2, 3, 4)] decimal parameter);
private delegate decimal DecimalWithDefaultValue(decimal parameter = 3.14m);
private static decimal DecimalMethod(decimal parameter)
{
return parameter;
}
private delegate int? NullableIntWithDefaultValue(int? parameter = null);
private static int? NullableIntMethod(int? parameter)
{
return parameter;
}
private delegate IntEnum EnumWithDefaultValue(IntEnum parameter = IntEnum.Seven);
private static IntEnum EnumMethod(IntEnum parameter = IntEnum.Seven)
{
return parameter;
}
private delegate object OptionalObjectParameter([Optional] object parameter);
private static object ObjectMethod(object parameter)
{
return parameter;
}
private delegate string OptionalStringParameter([Optional] string parameter);
}
public static class CreateDelegateTests
{
#region Tests
[Fact]
public static void CreateDelegate1_Method_Static()
{
C c = new C();
MethodInfo mi = typeof(C).GetMethod("S");
Delegate dg = Delegate.CreateDelegate(typeof(D), mi);
Assert.Equal(mi, dg.Method);
Assert.Null(dg.Target);
D d = (D)dg;
d(c);
}
[Fact]
public static void CreateDelegate1_Method_Null()
{
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("method", () => Delegate.CreateDelegate(typeof(D), (MethodInfo)null));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate1_Type_Null()
{
MethodInfo mi = typeof(C).GetMethod("S");
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("type", () => Delegate.CreateDelegate((Type)null, mi));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2()
{
E e;
e = (E)Delegate.CreateDelegate(typeof(E), new B(), "Execute");
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
e = (E)Delegate.CreateDelegate(typeof(E), new C(), "Execute");
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
e = (E)Delegate.CreateDelegate(typeof(E), new C(), "DoExecute");
Assert.NotNull(e);
Assert.Equal(102, e(new C()));
}
[Fact]
public static void CreateDelegate2_Method_ArgumentsMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "StartExecute"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2_Method_CaseMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "ExecutE"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2_Method_DoesNotExist()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "DoesNotExist"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2_Method_Null()
{
C c = new C();
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("method", () => Delegate.CreateDelegate(typeof(D), c, (string)null));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2_Method_ReturnTypeMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "DoExecute"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2_Method_Static()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "Run"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2_Target_Null()
{
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("target", () => Delegate.CreateDelegate(typeof(D), null, "N"));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate2_Target_GenericTypeParameter()
{
Type theT = typeof(DummyGenericClassForDelegateTests<>).GetTypeInfo().GenericTypeParameters[0];
Type delegateType = typeof(Func<object, object, bool>);
AssertExtensions.Throws<ArgumentException>("target", () => Delegate.CreateDelegate(delegateType, theT, "ReferenceEquals"));
}
[Fact]
public static void CreateDelegate2_Type_Null()
{
C c = new C();
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("type", () => Delegate.CreateDelegate((Type)null, c, "N"));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3()
{
E e;
// matching static method
e = (E)Delegate.CreateDelegate(typeof(E), typeof(B), "Run");
Assert.NotNull(e);
Assert.Equal(5, e(new C()));
// matching static method
e = (E)Delegate.CreateDelegate(typeof(E), typeof(C), "Run");
Assert.NotNull(e);
Assert.Equal(5, e(new C()));
// matching static method
e = (E)Delegate.CreateDelegate(typeof(E), typeof(C), "DoRun");
Assert.NotNull(e);
Assert.Equal(107, e(new C()));
}
[Fact]
public static void CreateDelegate3_Method_ArgumentsMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), typeof(B), "StartRun"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3_Method_CaseMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), typeof(B), "RuN"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3_Method_DoesNotExist()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), typeof(B), "DoesNotExist"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3_Method_Instance()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), typeof(B), "Execute"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3_Method_Null()
{
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("method", () => Delegate.CreateDelegate(typeof(D), typeof(C), (string)null));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3_Method_ReturnTypeMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), typeof(B), "DoRun"));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3_Target_Null()
{
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("target", () => Delegate.CreateDelegate(typeof(D), (Type)null, "S"));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate3_Type_Null()
{
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("type", () => Delegate.CreateDelegate((Type)null, typeof(C), "S"));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4()
{
E e;
B b = new B();
// instance method, exact case, ignore case
e = (E)Delegate.CreateDelegate(typeof(E), b, "Execute", true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// instance method, exact case, do not ignore case
e = (E)Delegate.CreateDelegate(typeof(E), b, "Execute", false);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// instance method, case mismatch, ignore case
e = (E)Delegate.CreateDelegate(typeof(E), b, "ExecutE", true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
C c = new C();
// instance method, exact case, ignore case
e = (E)Delegate.CreateDelegate(typeof(E), c, "Execute", true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// instance method, exact case, ignore case
e = (E)Delegate.CreateDelegate(typeof(E), c, "DoExecute", true);
Assert.NotNull(e);
Assert.Equal(102, e(new C()));
// instance method, exact case, do not ignore case
e = (E)Delegate.CreateDelegate(typeof(E), c, "Execute", false);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// instance method, case mismatch, ignore case
e = (E)Delegate.CreateDelegate(typeof(E), c, "ExecutE", true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
}
[Fact]
public static void CreateDelegate4_Method_ArgumentsMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "StartExecute", false));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4_Method_CaseMismatch()
{
// instance method, case mismatch, do not igore case
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "ExecutE", false));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4_Method_DoesNotExist()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "DoesNotExist", false));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4_Method_Null()
{
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("method", () => Delegate.CreateDelegate(typeof(D), new C(), (string)null, true));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4_Method_ReturnTypeMismatch()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "DoExecute", false));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4_Method_Static()
{
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "Run", true));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4_Target_Null()
{
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("target", () => Delegate.CreateDelegate(typeof(D), null, "N", true));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate4_Type_Null()
{
C c = new C();
ArgumentNullException ex = AssertExtensions.Throws<ArgumentNullException>("type", () => Delegate.CreateDelegate((Type)null, c, "N", true));
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
}
[Fact]
public static void CreateDelegate9()
{
E e;
// do not ignore case, do not throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new B(),
"Execute", false, false);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// do not ignore case, throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new B(),
"Execute", false, true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// ignore case, do not throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new B(),
"Execute", true, false);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// ignore case, throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new B(),
"Execute", true, true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// do not ignore case, do not throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"Execute", false, false);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// do not ignore case, throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"Execute", false, true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// ignore case, do not throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"Execute", true, false);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// ignore case, throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"Execute", true, true);
Assert.NotNull(e);
Assert.Equal(4, e(new C()));
// do not ignore case, do not throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"DoExecute", false, false);
Assert.NotNull(e);
Assert.Equal(102, e(new C()));
// do not ignore case, throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"DoExecute", false, true);
Assert.NotNull(e);
Assert.Equal(102, e(new C()));
// ignore case, do not throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"DoExecute", true, false);
Assert.NotNull(e);
Assert.Equal(102, e(new C()));
// ignore case, throw bind failure
e = (E)Delegate.CreateDelegate(typeof(E), new C(),
"DoExecute", true, true);
Assert.NotNull(e);
Assert.Equal(102, e(new C()));
}
[Fact]
public static void CreateDelegate9_Method_ArgumentsMismatch()
{
// throw bind failure
ArgumentException ex = AssertExtensions.Throws<ArgumentException>(null, () => Delegate.CreateDelegate(typeof(E), new B(), "StartExecute", false, true));
// Error binding to target method
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
// do not throw on bind failure
E e = (E)Delegate.CreateDelegate(typeof(E), new B(),
"StartExecute", false, false);
Assert.Null(e);
}