11using System ;
22using System . Collections . Concurrent ;
3+ using System . Collections . Generic ;
34using System . Collections . Immutable ;
45using System . Linq ;
56using System . Reflection ;
@@ -12,7 +13,7 @@ public static class HandlerTypeDescriptorHelper
1213 private static readonly ConcurrentDictionary < Type , string > MethodNames =
1314 new ConcurrentDictionary < Type , string > ( ) ;
1415
15- internal static readonly ImmutableSortedDictionary < string , IHandlerTypeDescriptor > KnownHandlers ;
16+ internal static readonly ILookup < string , IHandlerTypeDescriptor > KnownHandlers ;
1617
1718 static HandlerTypeDescriptorHelper ( )
1819 {
@@ -31,43 +32,38 @@ static HandlerTypeDescriptorHelper()
3132 }
3233 }
3334 )
34- . Where ( z => z . IsInterface && typeof ( IJsonRpcHandler ) . IsAssignableFrom ( z ) )
35+ . Where ( z => ( z . IsInterface || ( z . IsClass && ! z . IsAbstract ) ) && typeof ( IJsonRpcHandler ) . IsAssignableFrom ( z ) )
3536 . Where ( z => MethodAttribute . From ( z ) != null )
3637 . Where ( z => ! z . Name . EndsWith ( "Manager" ) ) // Manager interfaces are generally specializations around the handlers
3738 . Select ( GetMethodType )
3839 . Distinct ( )
3940 . ToLookup ( x => MethodAttribute . From ( x ) . Method )
40- . Select ( x => new HandlerTypeDescriptor ( x . First ( ) ) as IHandlerTypeDescriptor )
41- . ToImmutableSortedDictionary ( x => x . Method , x => x , StringComparer . Ordinal ) ;
41+ . SelectMany ( x => x . Select ( z => new HandlerTypeDescriptor ( z ) as IHandlerTypeDescriptor ) )
42+ . ToLookup ( x => x . Method , StringComparer . Ordinal ) ;
4243 }
4344 catch ( Exception e )
4445 {
4546 throw new AggregateException ( "Failed" , e ) ;
4647 }
4748 }
4849
49- public static IHandlerTypeDescriptor GetHandlerTypeDescriptor ( string method ) => KnownHandlers . TryGetValue ( method , out var descriptor ) ? descriptor : null ;
50-
51- public static IHandlerTypeDescriptor GetHandlerTypeDescriptor < T > ( ) =>
52- KnownHandlers . Values . FirstOrDefault ( x => x . InterfaceType == typeof ( T ) ) ??
53- GetHandlerTypeDescriptor ( GetMethodName ( typeof ( T ) ) ) ;
50+ public static IHandlerTypeDescriptor GetHandlerTypeDescriptor < T > ( ) => GetHandlerTypeDescriptor ( typeof ( T ) ) ;
5451
5552 public static IHandlerTypeDescriptor GetHandlerTypeDescriptor ( Type type )
5653 {
57- var @default = KnownHandlers . Values . FirstOrDefault ( x => x . InterfaceType == type ) ;
54+ var @default = KnownHandlers
55+ . SelectMany ( g => g )
56+ . FirstOrDefault ( x => x . InterfaceType == type || x . HandlerType == type || x . ParamsType == type ) ;
5857 if ( @default != null )
5958 {
6059 return @default ;
6160 }
6261
6362 var methodName = GetMethodName ( type ) ;
64- if ( string . IsNullOrWhiteSpace ( methodName ) ) return null ;
65- return GetHandlerTypeDescriptor ( methodName ) ;
63+ return string . IsNullOrWhiteSpace ( methodName ) ? null : KnownHandlers [ methodName ] . FirstOrDefault ( ) ;
6664 }
6765
68- public static string GetMethodName < T > ( )
69- where T : IJsonRpcHandler =>
70- GetMethodName ( typeof ( T ) ) ;
66+ public static string GetMethodName < T > ( ) where T : IJsonRpcHandler => GetMethodName ( typeof ( T ) ) ;
7167
7268 public static bool IsMethodName ( string name , params Type [ ] types ) => types . Any ( z => GetMethodName ( z ) . Equals ( name ) ) ;
7369
@@ -78,16 +74,13 @@ public static string GetMethodName(Type type)
7874 // Custom method
7975 var attribute = MethodAttribute . From ( type ) ;
8076
81- var handler = KnownHandlers . Values . FirstOrDefault (
82- z =>
83- z . InterfaceType == type || z . HandlerType == type || z . ParamsType == type
84- ) ;
77+ var handler = KnownHandlers . SelectMany ( z => z )
78+ . FirstOrDefault ( z => z . InterfaceType == type || z . HandlerType == type || z . ParamsType == type ) ;
8579 if ( handler != null )
8680 {
8781 return handler . Method ;
8882 }
8983
90-
9184 // TODO: Log unknown method name
9285 if ( attribute is null )
9386 {
0 commit comments