forked from castleproject/Core
/
ProxyGenerator.cs
1562 lines (1481 loc) · 107 KB
/
ProxyGenerator.cs
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
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Castle.DynamicProxy
{
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Castle.Core.Internal;
using Castle.Core.Logging;
/// <summary>
/// Provides proxy objects for classes and interfaces.
/// </summary>
[CLSCompliant(true)]
public class ProxyGenerator : IProxyGenerator
{
private ILogger logger = NullLogger.Instance;
private readonly IProxyBuilder proxyBuilder;
/// <summary>
/// Initializes a new instance of the <see cref = "ProxyGenerator" /> class.
/// </summary>
/// <param name = "builder">Proxy types builder.</param>
public ProxyGenerator(IProxyBuilder builder)
{
proxyBuilder = builder;
Logger = new TraceLogger("Castle.DynamicProxy", LoggerLevel.Warn);
}
/// <summary>
/// Initializes a new instance of the <see cref = "ProxyGenerator" /> class.
/// </summary>
public ProxyGenerator() : this(new DefaultProxyBuilder())
{
}
/// <summary>
/// Initializes a new instance of the <see cref = "ProxyGenerator" /> class.
/// </summary>
/// <param name="disableSignedModule">If <c>true</c> forces all types to be generated into an unsigned module.</param>
public ProxyGenerator(bool disableSignedModule) : this(new DefaultProxyBuilder(new ModuleScope(false, disableSignedModule)))
{
}
/// <summary>
/// Gets or sets the <see cref = "ILogger" /> that this <see cref = "ProxyGenerator" /> log to.
/// </summary>
public ILogger Logger
{
get { return logger; }
set
{
logger = value;
proxyBuilder.Logger = value;
}
}
/// <summary>
/// Gets the proxy builder instance used to generate proxy types.
/// </summary>
/// <value>The proxy builder.</value>
public IProxyBuilder ProxyBuilder
{
get { return proxyBuilder; }
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <typeparamref name = "TInterface" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// </summary>
/// <typeparam name = "TInterface">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</typeparam>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>Object proxying calls to members of <typeparamref name = "TInterface" /> on <paramref name = "target" /> object.</returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TInterface" />is not an interface type.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method generates new proxy type for each type of <paramref name = "target" />, which affects performance. If you don't want to proxy types differently depending on the type of the target
/// use <see cref = "CreateInterfaceProxyWithTargetInterface{TInterface}(TInterface,IInterceptor[])" /> method.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TInterface CreateInterfaceProxyWithTarget<TInterface>(TInterface target, params IInterceptor[] interceptors)
where TInterface : class
{
// NOTE: we don't need to document exception case where interface type is null, since it can never be for a generic method.
// If we leave target as being of type TInterface we also have covered exception where target does not implement TInterface.
// NOTE: Can any other Activator.CreateInstance exception be thrown in this context?
return
(TInterface)
CreateInterfaceProxyWithTarget(typeof(TInterface), target, ProxyGenerationOptions.Default, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <typeparamref name = "TInterface" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// </summary>
/// <typeparam name = "TInterface">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</typeparam>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <typeparamref name = "TInterface" /> on <paramref name = "target" /> object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TInterface" />is not an interface type.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method generates new proxy type for each type of <paramref name = "target" />, which affects performance. If you don't want to proxy types differently depending on the type of the target
/// use <see
/// cref = "CreateInterfaceProxyWithTargetInterface{TInterface}(TInterface,Castle.DynamicProxy.ProxyGenerationOptions,IInterceptor[])" /> method.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TInterface CreateInterfaceProxyWithTarget<TInterface>(TInterface target, ProxyGenerationOptions options,
params IInterceptor[] interceptors)
where TInterface : class
{
return (TInterface)CreateInterfaceProxyWithTarget(typeof(TInterface), target, options, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> type on <paramref name = "target" /> object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref
/// name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method generates new proxy type for each type of <paramref name = "target" />, which affects performance. If you don't want to proxy types differently depending on the type of the target
/// use <see cref = "CreateInterfaceProxyWithTargetInterface(Type,object,IInterceptor[])" /> method.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithTarget(Type interfaceToProxy, object target, params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithTarget(interfaceToProxy, target, ProxyGenerationOptions.Default, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> type on <paramref name = "target" /> object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref
/// name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method generates new proxy type for each type of <paramref name = "target" />, which affects performance. If you don't want to proxy types differently depending on the type of the target
/// use <see cref = "CreateInterfaceProxyWithTargetInterface(Type,object,ProxyGenerationOptions,IInterceptor[])" /> method.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithTarget(Type interfaceToProxy, object target, ProxyGenerationOptions options,
params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithTarget(interfaceToProxy, null, target, options, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "additionalInterfacesToProxy">Additional interface types. Calls to their members will be proxied as well.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> and <paramref
/// name = "additionalInterfacesToProxy" /> types on <paramref name = "target" /> object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> or any of <paramref
/// name = "additionalInterfacesToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref
/// name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method generates new proxy type for each type of <paramref name = "target" />, which affects performance. If you don't want to proxy types differently depending on the type of the target
/// use <see cref = "CreateInterfaceProxyWithTargetInterface(Type,Type[],object,IInterceptor[])" /> method.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, object target,
params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithTarget(interfaceToProxy, additionalInterfacesToProxy, target,
ProxyGenerationOptions.Default, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "additionalInterfacesToProxy">Additional interface types. Calls to their members will be proxied as well.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> and <paramref
/// name = "additionalInterfacesToProxy" /> types on <paramref name = "target" /> object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> or any of <paramref
/// name = "additionalInterfacesToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref
/// name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method generates new proxy type for each type of <paramref name = "target" />, which affects performance. If you don't want to proxy types differently depending on the type of the target
/// use <see cref = "CreateInterfaceProxyWithTargetInterface(Type,Type[],object,ProxyGenerationOptions,IInterceptor[])" /> method.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public virtual object CreateInterfaceProxyWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
object target,
ProxyGenerationOptions options,
params IInterceptor[] interceptors)
{
if (interfaceToProxy == null)
{
throw new ArgumentNullException(nameof(interfaceToProxy));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
if (interceptors == null)
{
throw new ArgumentNullException(nameof(interceptors));
}
if (!interfaceToProxy.IsInterface)
{
throw new ArgumentException("Specified type is not an interface", nameof(interfaceToProxy));
}
var targetType = target.GetType();
if (!interfaceToProxy.IsAssignableFrom(targetType))
{
throw new ArgumentException("Target does not implement interface " + interfaceToProxy.FullName, nameof(target));
}
CheckNotGenericTypeDefinition(interfaceToProxy, nameof(interfaceToProxy));
CheckNotGenericTypeDefinitions(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy));
var generatedType = CreateInterfaceProxyTypeWithTarget(interfaceToProxy, additionalInterfacesToProxy, targetType,
options);
var arguments = GetConstructorArguments(target, interceptors, options);
return Activator.CreateInstance(generatedType, arguments.ToArray());
}
protected List<object> GetConstructorArguments(object target, IInterceptor[] interceptors,
ProxyGenerationOptions options)
{
// create constructor arguments (initialized with mixin implementations, interceptors and target type constructor arguments)
var arguments = new List<object>(options.MixinData.Mixins) { interceptors, target };
if (options.Selector != null)
{
arguments.Add(options.Selector);
}
return arguments;
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// Interceptors can use <see cref = "IChangeProxyTarget" /> interface to provide other target for method invocation than default <paramref
/// name = "target" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> type on <paramref name = "target" /> object or alternative implementation swapped at runtime by an interceptor.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref
/// name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithTargetInterface(Type interfaceToProxy, object target,
params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithTargetInterface(interfaceToProxy, target, ProxyGenerationOptions.Default, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <typeparamref name = "TInterface" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// Interceptors can use <see cref = "IChangeProxyTarget" /> interface to provide other target for method invocation than default <paramref
/// name = "target" />.
/// </summary>
/// <typeparam name = "TInterface">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</typeparam>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <typeparamref name = "TInterface" /> type on <paramref name = "target" /> object or alternative implementation swapped at runtime by an interceptor.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TInterface" /> is not an interface type.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TInterface CreateInterfaceProxyWithTargetInterface<TInterface>(TInterface target,
params IInterceptor[] interceptors)
where TInterface : class
{
return (TInterface)CreateInterfaceProxyWithTargetInterface(typeof(TInterface),
target,
ProxyGenerationOptions.Default,
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <typeparamref name = "TInterface" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// Interceptors can use <see cref = "IChangeProxyTarget" /> interface to provide other target for method invocation than default <paramref
/// name = "target" />.
/// </summary>
/// <typeparam name = "TInterface">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</typeparam>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <typeparamref name = "TInterface" /> type on <paramref name = "target" /> object or alternative implementation swapped at runtime by an interceptor.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TInterface" /> is not an interface type.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TInterface CreateInterfaceProxyWithTargetInterface<TInterface>(TInterface target,
ProxyGenerationOptions options,
params IInterceptor[] interceptors)
where TInterface : class
{
return (TInterface)CreateInterfaceProxyWithTargetInterface(typeof(TInterface),
target,
options,
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// Interceptors can use <see cref = "IChangeProxyTarget" /> interface to provide other target for method invocation than default <paramref
/// name = "target" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "additionalInterfacesToProxy">Additional interface types. Calls to their members will be proxied as well.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> and <paramref
/// name = "additionalInterfacesToProxy" /> types on <paramref name = "target" /> object or alternative implementation swapped at runtime by an interceptor.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> or any of <paramref
/// name = "additionalInterfacesToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref
/// name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithTargetInterface(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
object target, params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithTargetInterface(interfaceToProxy, additionalInterfacesToProxy, target,
ProxyGenerationOptions.Default, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref
/// name = "target" /> object with given <paramref name = "interceptors" />.
/// Interceptors can use <see cref = "IChangeProxyTarget" /> interface to provide other target for method invocation than default <paramref
/// name = "target" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> type on <paramref name = "target" /> object or alternative implementation swapped at runtime by an interceptor.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref
/// name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref
/// name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref
/// name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithTargetInterface(Type interfaceToProxy, object target,
ProxyGenerationOptions options,
params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithTargetInterface(interfaceToProxy, null, target, options, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on <paramref name = "target" /> object with given <paramref name = "interceptors" />.
/// Interceptors can use <see cref = "IChangeProxyTarget" /> interface to provide other target for method invocation than default <paramref name = "target" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface implemented by <paramref name = "target" /> which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "additionalInterfacesToProxy">Additional interface types. Calls to their members will be proxied as well.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> and <paramref name = "additionalInterfacesToProxy" /> types on <paramref name = "target" /> object or alternative implementation swapped at runtime by an interceptor.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "target" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> or any of <paramref name = "additionalInterfacesToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "target" /> does not implement <paramref name = "interfaceToProxy" /> interface.</exception>
/// <exception cref = "MissingMethodException">Thrown when no default constructor exists on actual type of <paramref name = "target" /> object.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of actual type of <paramref name = "target" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public virtual object CreateInterfaceProxyWithTargetInterface(Type interfaceToProxy,
Type[] additionalInterfacesToProxy,
object target, ProxyGenerationOptions options,
params IInterceptor[] interceptors)
{
//TODO: add <example> to xml comments to show how to use IChangeProxyTarget
if (interfaceToProxy == null)
{
throw new ArgumentNullException(nameof(interfaceToProxy));
}
// In the case of a transparent proxy, the call to IsInstanceOfType was executed on the real object.
if (target != null && interfaceToProxy.IsInstanceOfType(target) == false)
{
throw new ArgumentException("Target does not implement interface " + interfaceToProxy.FullName, nameof(target));
}
if (interceptors == null)
{
throw new ArgumentNullException(nameof(interceptors));
}
if (!interfaceToProxy.IsInterface)
{
throw new ArgumentException("Specified type is not an interface", nameof(interfaceToProxy));
}
if (target != null)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Marshal.IsComObject(target))
{
var interfaceId = interfaceToProxy.GUID;
if (interfaceId != Guid.Empty)
{
var iUnknown = Marshal.GetIUnknownForObject(target); // Increment the reference count
var interfacePointer = IntPtr.Zero;
var result = Marshal.QueryInterface(iUnknown, ref interfaceId, out interfacePointer); // Increment the reference count
var isInterfacePointerNull = interfacePointer == IntPtr.Zero;
Marshal.Release(iUnknown); // Decrement the reference count
if (!isInterfacePointerNull)
{
Marshal.Release(interfacePointer); // Decrement the reference count
}
if (result == 0 && isInterfacePointerNull)
{
throw new ArgumentException("Target COM object does not implement interface " + interfaceToProxy.FullName,
nameof(target));
}
}
}
}
CheckNotGenericTypeDefinition(interfaceToProxy, nameof(interfaceToProxy));
CheckNotGenericTypeDefinitions(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy));
var generatedType = CreateInterfaceProxyTypeWithTargetInterface(interfaceToProxy, additionalInterfacesToProxy,
options);
var arguments = GetConstructorArguments(target, interceptors, options);
return Activator.CreateInstance(generatedType, arguments.ToArray());
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <typeparamref name = "TInterface" /> on target object generated at runtime with given <paramref
/// name = "interceptor" />.
/// </summary>
/// <typeparam name = "TInterface">Type of the interface which will be proxied.</typeparam>
/// <param name = "interceptor">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <typeparamref name = "TInterface" /> types on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptor" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TInterface" /> is not an interface type.</exception>
/// <remarks>
/// Since this method uses an empty-shell implementation of interfaces to proxy generated at runtime, the actual implementation of proxied methods must be provided by given <see
/// cref = "IInterceptor" /> implementations.
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// As a result of that also at least one <see cref = "IInterceptor" /> implementation must be provided.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TInterface CreateInterfaceProxyWithoutTarget<TInterface>(IInterceptor interceptor)
where TInterface : class
{
return (TInterface)CreateInterfaceProxyWithoutTarget(typeof(TInterface), interceptor);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <typeparamref name = "TInterface" /> on target object generated at runtime with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <typeparam name = "TInterface">Type of the interface which will be proxied.</typeparam>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <typeparamref name = "TInterface" /> types on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TInterface" /> is not an interface type.</exception>
/// <remarks>
/// Since this method uses an empty-shell implementation of interfaces to proxy generated at runtime, the actual implementation of proxied methods must be provided by given <see
/// cref = "IInterceptor" /> implementations.
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// As a result of that also at least one <see cref = "IInterceptor" /> implementation must be provided.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TInterface CreateInterfaceProxyWithoutTarget<TInterface>(params IInterceptor[] interceptors)
where TInterface : class
{
return (TInterface)CreateInterfaceProxyWithoutTarget(typeof(TInterface), interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <typeparamref name = "TInterface" /> on target object generated at runtime with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <typeparam name = "TInterface">Type of the interface which will be proxied.</typeparam>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <typeparamref name = "TInterface" /> types on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TInterface" /> is not an interface type.</exception>
/// <remarks>
/// Since this method uses an empty-shell implementation of interfaces to proxy generated at runtime, the actual implementation of proxied methods must be provided by given <see
/// cref = "IInterceptor" /> implementations.
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// As a result of that also at least one <see cref = "IInterceptor" /> implementation must be provided.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TInterface CreateInterfaceProxyWithoutTarget<TInterface>(ProxyGenerationOptions options,
params IInterceptor[] interceptors)
where TInterface : class
{
return (TInterface)CreateInterfaceProxyWithoutTarget(typeof(TInterface), Type.EmptyTypes, options, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on target object generated at runtime with given <paramref
/// name = "interceptor" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface which will be proxied.</param>
/// <param name = "interceptor">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> type on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptor" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <remarks>
/// Since this method uses an empty-shell implementation of interfaces to proxy generated at runtime, the actual implementation of proxied methods must be provided by given <see
/// cref = "IInterceptor" /> implementations.
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, IInterceptor interceptor)
{
return CreateInterfaceProxyWithoutTarget(interfaceToProxy, Type.EmptyTypes, ProxyGenerationOptions.Default,
interceptor);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on target object generated at runtime with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface which will be proxied.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> type on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <remarks>
/// Since this method uses an empty-shell implementation of interfaces to proxy generated at runtime, the actual implementation of proxied methods must be provided by given <see
/// cref = "IInterceptor" /> implementations.
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithoutTarget(interfaceToProxy, Type.EmptyTypes, ProxyGenerationOptions.Default,
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on target object generated at runtime with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface which will be proxied.</param>
/// <param name = "additionalInterfacesToProxy">Additional interface types. Calls to their members will be proxied as well.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> and <paramref
/// name = "additionalInterfacesToProxy" /> types on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> or any of <paramref
/// name = "additionalInterfacesToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <remarks>
/// Since this method uses an empty-shell implementation of interfaces to proxy generated at runtime, the actual implementation of proxied methods must be provided by given <see
/// cref = "IInterceptor" /> implementations.
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithoutTarget(interfaceToProxy, additionalInterfacesToProxy,
ProxyGenerationOptions.Default, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on target object generated at runtime with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface which will be proxied.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <remarks>
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, ProxyGenerationOptions options,
params IInterceptor[] interceptors)
{
return CreateInterfaceProxyWithoutTarget(interfaceToProxy, Type.EmptyTypes, options, interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to members of interface <paramref name = "interfaceToProxy" /> on target object generated at runtime with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "interfaceToProxy">Type of the interface which will be proxied.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "additionalInterfacesToProxy">Additional interface types. Calls to their members will be proxied as well.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// Object proxying calls to members of <paramref name = "interfaceToProxy" /> and <paramref
/// name = "additionalInterfacesToProxy" /> types on generated target object.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interfaceToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "interceptors" /> array is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> or any of <paramref
/// name = "additionalInterfacesToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "interfaceToProxy" /> is not an interface type.</exception>
/// <remarks>
/// Since this method uses an empty-shell implementation of <paramref name = "additionalInterfacesToProxy" /> to proxy generated at runtime, the actual implementation of proxied methods must be provided by given <see
/// cref = "IInterceptor" /> implementations.
/// They are responsible for setting return value (and out parameters) on proxied methods. It is also illegal for an interceptor to call <see
/// cref = "IInvocation.Proceed" />, since there's no actual implementation to proceed with.
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public virtual object CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options,
params IInterceptor[] interceptors)
{
if (interfaceToProxy == null)
{
throw new ArgumentNullException(nameof(interfaceToProxy));
}
if (interceptors == null)
{
throw new ArgumentNullException(nameof(interceptors));
}
if (!interfaceToProxy.IsInterface)
{
throw new ArgumentException("Specified type is not an interface", nameof(interfaceToProxy));
}
CheckNotGenericTypeDefinition(interfaceToProxy, nameof(interfaceToProxy));
CheckNotGenericTypeDefinitions(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy));
var generatedType = CreateInterfaceProxyTypeWithoutTarget(interfaceToProxy, additionalInterfacesToProxy, options);
var arguments = GetConstructorArguments(null, interceptors, options);
return Activator.CreateInstance(generatedType, arguments.ToArray());
}
/// <summary>
/// Creates proxy object intercepting calls to virtual members of type <typeparamref name = "TClass" /> on newly created instance of that type with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <typeparam name = "TClass">Type of class which will be proxied.</typeparam>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// New object of type <typeparamref name = "TClass" /> proxying calls to virtual members of <typeparamref
/// name = "TClass" /> type.
/// </returns>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TClass" /> is not a class type.</exception>
/// <exception cref = "ArgumentException">Thrown when no default constructor exists on type <typeparamref name = "TClass" />.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of type <typeparamref name = "TClass" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TClass CreateClassProxyWithTarget<TClass>(TClass target, params IInterceptor[] interceptors)
where TClass : class
{
return (TClass)CreateClassProxyWithTarget(typeof(TClass),
Type.EmptyTypes,
target,
ProxyGenerationOptions.Default,
new object[0],
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to virtual members of type <typeparamref name = "TClass" /> on newly created instance of that type with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <typeparam name = "TClass">Type of class which will be proxied.</typeparam>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// New object of type <typeparamref name = "TClass" /> proxying calls to virtual members of <typeparamref
/// name = "TClass" /> type.
/// </returns>
/// <exception cref = "ArgumentException">Thrown when given <typeparamref name = "TClass" /> is not a class type.</exception>
/// <exception cref = "ArgumentException">Thrown when no default constructor exists on type <typeparamref name = "TClass" />.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of type <typeparamref name = "TClass" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public TClass CreateClassProxyWithTarget<TClass>(TClass target, ProxyGenerationOptions options,
params IInterceptor[] interceptors) where TClass : class
{
return (TClass)CreateClassProxyWithTarget(typeof(TClass),
Type.EmptyTypes,
target,
options,
new object[0],
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to virtual members of type <paramref name = "classToProxy" /> on newly created instance of that type with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "classToProxy">Type of class which will be proxied.</param>
/// <param name = "additionalInterfacesToProxy">Additional interface types. Calls to their members will be proxied as well.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// New object of type <paramref name = "classToProxy" /> proxying calls to virtual members of <paramref
/// name = "classToProxy" /> and <paramref name = "additionalInterfacesToProxy" /> types.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "classToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "classToProxy" /> or any of <paramref
/// name = "additionalInterfacesToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "classToProxy" /> is not a class type.</exception>
/// <exception cref = "ArgumentException">Thrown when no default constructor exists on type <paramref name = "classToProxy" />.</exception>
/// <exception cref = "TargetInvocationException">Thrown when default constructor of type <paramref name = "classToProxy" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateClassProxyWithTarget(Type classToProxy, Type[] additionalInterfacesToProxy, object target,
params IInterceptor[] interceptors)
{
return CreateClassProxyWithTarget(classToProxy,
additionalInterfacesToProxy,
target,
ProxyGenerationOptions.Default,
new object[0],
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to virtual members of type <paramref name = "classToProxy" /> on newly created instance of that type with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "classToProxy">Type of class which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "options">The proxy generation options used to influence generated proxy type and object.</param>
/// <param name = "constructorArguments">Arguments of constructor of type <paramref name = "classToProxy" /> which should be used to create a new instance of that type.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// New object of type <paramref name = "classToProxy" /> proxying calls to virtual members of <paramref
/// name = "classToProxy" /> type.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "classToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "classToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "classToProxy" /> is not a class type.</exception>
/// <exception cref = "ArgumentException">Thrown when no constructor exists on type <paramref name = "classToProxy" /> with parameters matching <paramref
/// name = "constructorArguments" />.</exception>
/// <exception cref = "TargetInvocationException">Thrown when constructor of type <paramref name = "classToProxy" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateClassProxyWithTarget(Type classToProxy, object target, ProxyGenerationOptions options,
object[] constructorArguments, params IInterceptor[] interceptors)
{
return CreateClassProxyWithTarget(classToProxy,
Type.EmptyTypes,
target,
options,
constructorArguments,
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to virtual members of type <paramref name = "classToProxy" /> on newly created instance of that type with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "classToProxy">Type of class which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "constructorArguments">Arguments of constructor of type <paramref name = "classToProxy" /> which should be used to create a new instance of that type.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>
/// New object of type <paramref name = "classToProxy" /> proxying calls to virtual members of <paramref
/// name = "classToProxy" /> type.
/// </returns>
/// <exception cref = "ArgumentNullException">Thrown when given <paramref name = "classToProxy" /> object is a null reference (Nothing in Visual Basic).</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "classToProxy" /> is a generic type definition.</exception>
/// <exception cref = "ArgumentException">Thrown when given <paramref name = "classToProxy" /> is not a class type.</exception>
/// <exception cref = "ArgumentException">Thrown when no constructor exists on type <paramref name = "classToProxy" /> with parameters matching <paramref
/// name = "constructorArguments" />.</exception>
/// <exception cref = "TargetInvocationException">Thrown when constructor of type <paramref name = "classToProxy" /> throws an exception.</exception>
/// <remarks>
/// This method uses <see cref = "IProxyBuilder" /> implementation to generate a proxy type.
/// As such caller should expect any type of exception that given <see cref = "IProxyBuilder" /> implementation may throw.
/// </remarks>
public object CreateClassProxyWithTarget(Type classToProxy, object target, object[] constructorArguments,
params IInterceptor[] interceptors)
{
return CreateClassProxyWithTarget(classToProxy,
Type.EmptyTypes,
target,
ProxyGenerationOptions.Default,
constructorArguments,
interceptors);
}
/// <summary>
/// Creates proxy object intercepting calls to virtual members of type <paramref name = "classToProxy" /> on newly created instance of that type with given <paramref
/// name = "interceptors" />.
/// </summary>
/// <param name = "classToProxy">Type of class which will be proxied.</param>
/// <param name = "target">The target object, calls to which will be intercepted.</param>
/// <param name = "interceptors">The interceptors called during the invocation of proxied methods.</param>
/// <returns>