11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Reflection ;
45
@@ -87,9 +88,9 @@ private CastleForwardingInterceptor CreateForwardingInterceptor(ICallRouter call
8788 }
8889
8990 private object CreateProxyUsingCastleProxyGenerator ( Type typeToProxy , Type [ ] ? additionalInterfaces ,
90- object ? [ ] ? constructorArguments ,
91- IInterceptor [ ] interceptors ,
92- ProxyGenerationOptions proxyGenerationOptions )
91+ object ? [ ] ? constructorArguments ,
92+ IInterceptor [ ] interceptors ,
93+ ProxyGenerationOptions proxyGenerationOptions )
9394 {
9495 additionalInterfaces ??= new Type [ ] { } ;
9596 var classToInstantiate = additionalInterfaces
@@ -100,39 +101,49 @@ private object CreateProxyUsingCastleProxyGenerator(Type typeToProxy, Type[]? ad
100101 . Where ( x => x . GetTypeInfo ( ) . IsInterface )
101102 . ToList ( ) ;
102103
104+
105+ if ( classToInstantiate != null &&
106+ typeToProxy . GetTypeInfo ( ) . IsInterface &&
107+ typeToProxy . IsAssignableFrom ( classToInstantiate ) )
108+ {
109+ return CreateProxyForClassImplementingInterface (
110+ typeToProxy ,
111+ constructorArguments ,
112+ interceptors ,
113+ proxyGenerationOptions ,
114+ classToInstantiate ,
115+ implementedInterfaces ) ;
116+ }
117+
118+ return CreateMixin (
119+ typeToProxy ,
120+ additionalInterfaces ,
121+ constructorArguments ,
122+ interceptors ,
123+ proxyGenerationOptions ,
124+ classToInstantiate ) ;
125+ }
126+
127+ private object CreateMixin ( Type typeToProxy , Type [ ] additionalInterfaces , object ? [ ] ? constructorArguments , IInterceptor [ ] interceptors , ProxyGenerationOptions proxyGenerationOptions , Type ? classToInstantiate )
128+ {
103129 if ( typeToProxy . GetTypeInfo ( ) . IsInterface )
104130 {
105- if ( classToInstantiate != null )
106- {
107- VerifyClassImplementsInterface ( classToInstantiate , typeToProxy ) ;
108- VerifyClassIsNotAbstract ( classToInstantiate ) ;
109- var targetObject = Activator . CreateInstance ( classToInstantiate , constructorArguments ) ;
110- implementedInterfaces . Add ( typeToProxy ) ;
111-
112- return _proxyGenerator . CreateInterfaceProxyWithTarget ( typeToProxy ,
113- implementedInterfaces . ToArray ( ) ,
114- target : targetObject ,
115- options : proxyGenerationOptions ,
116- interceptors : interceptors ) ;
117- }
118- else
119- {
131+ if ( classToInstantiate == null )
120132 VerifyNoConstructorArgumentsGivenForInterface ( constructorArguments ) ;
121133
122- var interfacesArrayLength = additionalInterfaces != null ? additionalInterfaces . Length + 1 : 1 ;
123- var interfaces = new Type [ interfacesArrayLength ] ;
124-
125- interfaces [ 0 ] = typeToProxy ;
126- if ( additionalInterfaces != null )
127- {
128- Array . Copy ( additionalInterfaces , 0 , interfaces , 1 , additionalInterfaces . Length ) ;
129- }
134+ var interfacesArrayLength = additionalInterfaces != null ? additionalInterfaces . Length + 1 : 1 ;
135+ var interfaces = new Type [ interfacesArrayLength ] ;
130136
131- // We need to create a proxy for the object type, so we can intercept the ToString() method.
132- // Therefore, we put the desired primary interface to the secondary list.
133- typeToProxy = typeof ( object ) ;
134- additionalInterfaces = interfaces ;
137+ interfaces [ 0 ] = typeToProxy ;
138+ if ( additionalInterfaces != null )
139+ {
140+ Array . Copy ( additionalInterfaces , 0 , interfaces , 1 , additionalInterfaces . Length ) ;
135141 }
142+
143+ // We need to create a proxy for the object type, so we can intercept the ToString() method.
144+ // Therefore, we put the desired primary interface to the secondary list.
145+ typeToProxy = classToInstantiate ?? typeof ( object ) ;
146+ additionalInterfaces = interfaces ;
136147 }
137148
138149 return _proxyGenerator . CreateClassProxy ( typeToProxy ,
@@ -142,6 +153,18 @@ private object CreateProxyUsingCastleProxyGenerator(Type typeToProxy, Type[]? ad
142153 interceptors ) ;
143154 }
144155
156+ private object CreateProxyForClassImplementingInterface ( Type typeToProxy , object ? [ ] ? constructorArguments , IInterceptor [ ] interceptors , ProxyGenerationOptions proxyGenerationOptions , Type classToInstantiate , List < Type > implementedInterfaces )
157+ {
158+ VerifyClassIsNotAbstract ( classToInstantiate ) ;
159+ var targetObject = Activator . CreateInstance ( classToInstantiate , constructorArguments ) ;
160+ implementedInterfaces . Add ( typeToProxy ) ;
161+
162+ return _proxyGenerator . CreateInterfaceProxyWithTarget ( typeToProxy ,
163+ implementedInterfaces . ToArray ( ) ,
164+ target : targetObject ,
165+ options : proxyGenerationOptions ,
166+ interceptors : interceptors ) ;
167+ }
145168
146169 private ProxyGenerationOptions GetOptionsToMixinCallRouterProvider ( ICallRouter callRouter )
147170 {
0 commit comments