2
2
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.
3
3
4
4
using System ;
5
+ using System . Collections . Concurrent ;
5
6
using System . Diagnostics ;
6
7
using System . Reflection ;
7
8
8
9
#if FEATURE_DEFAULT_INTERFACE_IMPLEMENTATIONS
9
- using System . Collections . Concurrent ;
10
10
using System . Collections . Generic ;
11
11
using System . Linq ;
12
12
using System . Reflection . Emit ;
@@ -28,11 +28,19 @@ sealed class CastleProxyFactory : ProxyFactory
28
28
{
29
29
ProxyGenerationOptions generationOptions ;
30
30
ProxyGenerator generator ;
31
+ ConcurrentDictionary < string , ProxyGenerator > classGenerators ;
31
32
32
33
public CastleProxyFactory ( )
33
34
{
34
35
this . generationOptions = new ProxyGenerationOptions { Hook = new IncludeObjectMethodsHook ( ) , BaseTypeForInterfaceProxy = typeof ( InterfaceProxy ) } ;
35
36
this . generator = new ProxyGenerator ( ) ;
37
+ this . classGenerators = new ConcurrentDictionary < string , ProxyGenerator > ( ) ;
38
+ }
39
+
40
+ ProxyGenerator GetClassGenerator ( Type mockType )
41
+ {
42
+ var ns = mockType . Namespace ?? string . Empty ;
43
+ return classGenerators . GetOrAdd ( ns , _ => new ProxyGenerator ( ) ) ;
36
44
}
37
45
38
46
/// <inheritdoc />
@@ -53,13 +61,13 @@ public override object CreateProxy(Type mockType, Moq.IInterceptor interceptor,
53
61
{
54
62
var options = new ProxyGenerationOptions ( ) ;
55
63
options . AddDelegateTypeMixin ( mockType ) ;
56
- var container = generator . CreateClassProxy ( typeof ( object ) , additionalInterfaces , options , new Interceptor ( interceptor ) ) ;
64
+ var container = GetClassGenerator ( mockType ) . CreateClassProxy ( typeof ( object ) , additionalInterfaces , options , new Interceptor ( interceptor ) ) ;
57
65
return Delegate . CreateDelegate ( mockType , container , container . GetType ( ) . GetMethod ( "Invoke" ) ) ;
58
66
}
59
67
60
68
try
61
69
{
62
- return generator . CreateClassProxy ( mockType , additionalInterfaces , this . generationOptions , arguments , new Interceptor ( interceptor ) ) ;
70
+ return GetClassGenerator ( mockType ) . CreateClassProxy ( mockType , additionalInterfaces , this . generationOptions , arguments , new Interceptor ( interceptor ) ) ;
63
71
}
64
72
catch ( TypeLoadException e )
65
73
{
0 commit comments