Skip to content

Commit fd5dc34

Browse files
authored
Find private state properties if in base classes (Fixes #25) (#28)
1 parent 17a4198 commit fd5dc34

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

Docs/releases.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Fixed bug that caused exception when using `.ConfigureAwait` in an Effect ([#20](https://github.com/mrpmorris/Fluxor/issues/20))
55
* Ensured add/remove on events are thread safe ([#23](https://github.com/mrpmorris/Fluxor/issues/23))
66
* Made it easier to find the source of DisposableCallback instances that are not disposed ([#24](https://github.com/mrpmorris/Fluxor/issues/24))
7+
* State properties were not discovered if they were declared as private in a base class ([#25](https://github.com/mrpmorris/Fluxor/issues/25))
78

89
### New in 3.1.0
910
* Used Newtonsoft entirely for JS interop to ReduxDevTools to prevent serialization errors ([#7](https://github.com/mrpmorris/Fluxor/issues/7))

Source/Fluxor.Blazor.Web/Components/StateSubscriber.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static IDisposable Subscribe(object subject, Action<IState> callback)
2525

2626
IEnumerable<GetStateDelegate> getStateDelegates = GetStateDelegatesForType(subject.GetType());
2727
var subscriptions = new List<(IState State, EventHandler Handler)>();
28-
foreach(GetStateDelegate getState in getStateDelegates)
28+
foreach (GetStateDelegate getState in getStateDelegates)
2929
{
3030
var state = (IState)getState(subject);
3131
var handler = new EventHandler((s, a) => callback(state));
@@ -42,18 +42,24 @@ public static IDisposable Subscribe(object subject, Action<IState> callback)
4242
});
4343
}
4444

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+
4555
private static IEnumerable<GetStateDelegate> GetStateDelegatesForType(Type type)
4656
{
4757
return ValueDelegatesForType.GetOrAdd(type, _ =>
4858
{
4959
var delegates = new List<GetStateDelegate>();
60+
IEnumerable<PropertyInfo> stateProperties = GetStateProperties(type);
5061

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)
5763
{
5864
Type stateType = currentProperty.PropertyType.GetGenericArguments()[0];
5965
Type iStateType = typeof(IState<>).MakeGenericType(stateType);

0 commit comments

Comments
 (0)