1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
+ using System . Collections . Generic ;
3
4
using System . Collections . Immutable ;
4
5
using System . Linq ;
5
6
using System . Reflection ;
@@ -12,7 +13,7 @@ public static class HandlerTypeDescriptorHelper
12
13
private static readonly ConcurrentDictionary < Type , string > MethodNames =
13
14
new ConcurrentDictionary < Type , string > ( ) ;
14
15
15
- internal static readonly ImmutableSortedDictionary < string , IHandlerTypeDescriptor > KnownHandlers ;
16
+ internal static readonly ILookup < string , IHandlerTypeDescriptor > KnownHandlers ;
16
17
17
18
static HandlerTypeDescriptorHelper ( )
18
19
{
@@ -31,43 +32,38 @@ static HandlerTypeDescriptorHelper()
31
32
}
32
33
}
33
34
)
34
- . Where ( z => z . IsInterface && typeof ( IJsonRpcHandler ) . IsAssignableFrom ( z ) )
35
+ . Where ( z => ( z . IsInterface || ( z . IsClass && ! z . IsAbstract ) ) && typeof ( IJsonRpcHandler ) . IsAssignableFrom ( z ) )
35
36
. Where ( z => MethodAttribute . From ( z ) != null )
36
37
. Where ( z => ! z . Name . EndsWith ( "Manager" ) ) // Manager interfaces are generally specializations around the handlers
37
38
. Select ( GetMethodType )
38
39
. Distinct ( )
39
40
. 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 ) ;
42
43
}
43
44
catch ( Exception e )
44
45
{
45
46
throw new AggregateException ( "Failed" , e ) ;
46
47
}
47
48
}
48
49
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 ) ) ;
54
51
55
52
public static IHandlerTypeDescriptor GetHandlerTypeDescriptor ( Type type )
56
53
{
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 ) ;
58
57
if ( @default != null )
59
58
{
60
59
return @default ;
61
60
}
62
61
63
62
var methodName = GetMethodName ( type ) ;
64
- if ( string . IsNullOrWhiteSpace ( methodName ) ) return null ;
65
- return GetHandlerTypeDescriptor ( methodName ) ;
63
+ return string . IsNullOrWhiteSpace ( methodName ) ? null : KnownHandlers [ methodName ] . FirstOrDefault ( ) ;
66
64
}
67
65
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 ) ) ;
71
67
72
68
public static bool IsMethodName ( string name , params Type [ ] types ) => types . Any ( z => GetMethodName ( z ) . Equals ( name ) ) ;
73
69
@@ -78,16 +74,13 @@ public static string GetMethodName(Type type)
78
74
// Custom method
79
75
var attribute = MethodAttribute . From ( type ) ;
80
76
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 ) ;
85
79
if ( handler != null )
86
80
{
87
81
return handler . Method ;
88
82
}
89
83
90
-
91
84
// TODO: Log unknown method name
92
85
if ( attribute is null )
93
86
{
0 commit comments