@@ -25,7 +25,7 @@ public static IDisposable Subscribe(object subject, Action<IState> callback)
25
25
26
26
IEnumerable < GetStateDelegate > getStateDelegates = GetStateDelegatesForType ( subject . GetType ( ) ) ;
27
27
var subscriptions = new List < ( IState State , EventHandler Handler ) > ( ) ;
28
- foreach ( GetStateDelegate getState in getStateDelegates )
28
+ foreach ( GetStateDelegate getState in getStateDelegates )
29
29
{
30
30
var state = ( IState ) getState ( subject ) ;
31
31
var handler = new EventHandler ( ( s , a ) => callback ( state ) ) ;
@@ -42,18 +42,24 @@ public static IDisposable Subscribe(object subject, Action<IState> callback)
42
42
} ) ;
43
43
}
44
44
45
+ private static IEnumerable < PropertyInfo > GetStateProperties ( Type t ) =>
46
+ t == typeof ( object )
47
+ ? Enumerable . Empty < PropertyInfo > ( )
48
+ : GetStateProperties ( t . BaseType )
49
+ . Union (
50
+ t . GetProperties ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . DeclaredOnly )
51
+ . Where ( p => p . PropertyType . IsGenericType )
52
+ . Where ( p => p . PropertyType . GetGenericTypeDefinition ( ) == typeof ( IState < > ) )
53
+ ) ;
54
+
45
55
private static IEnumerable < GetStateDelegate > GetStateDelegatesForType ( Type type )
46
56
{
47
57
return ValueDelegatesForType . GetOrAdd ( type , _ =>
48
58
{
49
59
var delegates = new List < GetStateDelegate > ( ) ;
60
+ IEnumerable < PropertyInfo > stateProperties = GetStateProperties ( type ) ;
50
61
51
- const BindingFlags bindingFlags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ;
52
- IEnumerable < PropertyInfo > stateProperties = type . GetProperties ( bindingFlags )
53
- . Where ( p => p . PropertyType . IsGenericType )
54
- . Where ( p => p . PropertyType . GetGenericTypeDefinition ( ) == typeof ( IState < > ) ) ;
55
-
56
- foreach ( PropertyInfo currentProperty in stateProperties )
62
+ foreach ( PropertyInfo currentProperty in stateProperties )
57
63
{
58
64
Type stateType = currentProperty . PropertyType . GetGenericArguments ( ) [ 0 ] ;
59
65
Type iStateType = typeof ( IState < > ) . MakeGenericType ( stateType ) ;
0 commit comments