@@ -395,23 +395,24 @@ internal static void DefineInterfaceProxyMethods(Type interfaceType, Type target
395395 var covariantReturnMethods = targetType . GetCovariantReturnMethods ( ) ;
396396 foreach ( var method in interfaceType . GetTypeInfo ( ) . DeclaredMethods . Where ( x => ! x . IsPropertyBinding ( ) ) )
397397 {
398- var covariantReturnMethod = covariantReturnMethods
399- . FirstOrDefault ( m => m . InterfaceDeclarations . Contains ( method ) )
400- . CovariantReturnMethod ;
401-
398+ var covariantReturnMethod = GetCovariantReturnMethod ( method ) ;
402399 DefineInterfaceMethod ( method , targetType , typeDesc , covariantReturnMethod ) ;
403400 }
404401 foreach ( var item in additionalInterfaces )
405402 {
406403 foreach ( var method in item . GetTypeInfo ( ) . DeclaredMethods . Where ( x => ! x . IsPropertyBinding ( ) ) )
407404 {
408- var covariantReturnMethod = covariantReturnMethods
409- . FirstOrDefault ( m => m . InterfaceDeclarations . Contains ( method ) )
410- . CovariantReturnMethod ;
411-
405+ var covariantReturnMethod = GetCovariantReturnMethod ( method ) ;
412406 DefineExplicitMethod ( method , targetType , typeDesc , covariantReturnMethod ) ;
413407 }
414408 }
409+
410+ MethodInfo GetCovariantReturnMethod ( MethodInfo interfaceMethod )
411+ {
412+ return covariantReturnMethods
413+ . FirstOrDefault ( m => m . InterfaceDeclarations . Contains ( interfaceMethod ) )
414+ . CovariantReturnMethod ;
415+ }
415416 }
416417
417418 internal static void DefineClassProxyMethods ( Type serviceType , Type implType , Type [ ] additionalInterfaces , TypeDesc typeDesc )
@@ -421,15 +422,15 @@ internal static void DefineClassProxyMethods(Type serviceType, Type implType, Ty
421422 {
422423 if ( method . IsVisibleAndVirtual ( ) && ! _ignores . Contains ( method . Name ) )
423424 {
424- var covariantReturnMethod = covariantReturnMethods . FirstOrDefault ( m => m . OverridenMethod . IsSameBaseDefinition ( method ) ) ;
425- var overriden = covariantReturnMethod . OverridenMethod ;
425+ var covariantReturn = covariantReturnMethods . FirstOrDefault ( m => m . OverridenMethod . IsSameBaseDefinition ( method ) ) ;
426+ var overriden = covariantReturn . OverridenMethod ;
426427 if ( overriden != null )
427428 {
428429 // if method is the base definition of the overriden method, the CovariantReturnMethod is not in serviceType, so we need to add CovariantReturnMethod to implType.
429430 // otherwise, the CovariantReturnMethod is also in serviceType, which will be added to implType in next for-loops.
430431 if ( overriden . GetBaseDefinition ( ) == method )
431432 {
432- DefineClassMethod ( covariantReturnMethod . CovariantReturnMethod , implType , typeDesc ) ;
433+ DefineClassMethod ( covariantReturn . CovariantReturnMethod , implType , typeDesc ) ;
433434 }
434435
435436 // covariantReturnMethod is found, do not add method to implType.
@@ -767,23 +768,26 @@ public static void DefineInterfaceProxyProperties(Type interfaceType, Type implT
767768
768769 foreach ( var property in interfaceType . GetTypeInfo ( ) . DeclaredProperties )
769770 {
771+ var covariantReturnGetter = FindCovariantReturnGetter ( property ) ;
770772 var builder = DefineInterfaceProxyProperty ( property , property . Name , implType , typeDesc ) ;
771- var covariantReturnGetter = property . CanRead
772- ? covariantReturnMethods . FirstOrDefault ( m => m . InterfaceDeclarations . Contains ( property . GetMethod ) ) . CovariantReturnMethod
773- : null ;
774773 DefineInterfacePropertyMethod ( builder , property , implType , typeDesc , covariantReturnGetter ) ;
775774 }
776775 foreach ( var item in additionalInterfaces )
777776 {
778777 foreach ( var property in item . GetTypeInfo ( ) . DeclaredProperties )
779778 {
779+ var covariantReturnGetter = FindCovariantReturnGetter ( property ) ;
780780 var builder = DefineInterfaceProxyProperty ( property , property . GetDisplayName ( ) , implType , typeDesc ) ;
781- var covariantReturnGetter = property . CanRead
782- ? covariantReturnMethods . FirstOrDefault ( m => m . InterfaceDeclarations . Contains ( property . GetMethod ) ) . CovariantReturnMethod
783- : null ;
784781 DefineExplicitPropertyMethod ( builder , property , implType , typeDesc , covariantReturnGetter ) ;
785782 }
786783 }
784+
785+ MethodInfo FindCovariantReturnGetter ( PropertyInfo property )
786+ {
787+ return property . CanRead
788+ ? covariantReturnMethods . FirstOrDefault ( m => m . InterfaceDeclarations . Contains ( property . GetMethod ) ) . CovariantReturnMethod
789+ : null ;
790+ }
787791 }
788792
789793 internal static void DefineClassProxyProperties ( Type serviceType , Type implType , Type [ ] additionalInterfaces , TypeDesc typeDesc )
0 commit comments