Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of events declared on the base types of target objects #341

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/StreamJsonRpc.Tests/TargetObjectEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class TargetObjectEventsTests : TestBase
public TargetObjectEventsTests(ITestOutputHelper logger)
: base(logger)
{
this.server = new Server();
this.server = new ServerDerived();
this.client = new Client();

var streams = FullDuplexStream.CreateStreams();
Expand Down Expand Up @@ -268,6 +268,10 @@ public void TriggerGenericEvent(CustomEventArgs args)
protected virtual void OnServerEventWithCustomGenericDelegateAndArgs(MessageEventArgs<string> args) => this.ServerEventWithCustomGenericDelegateAndArgs?.Invoke(this, args);
}

private class ServerDerived : Server
{
}

private class ServerWithIncompatibleEvents
{
public delegate int MyDelegate(double d);
Expand Down
23 changes: 13 additions & 10 deletions src/StreamJsonRpc/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,21 +670,24 @@ public void AddLocalRpcTarget(object target, JsonRpcTargetOptions options)

if (options.NotifyClientOfEvents)
{
foreach (var evt in target.GetType().GetTypeInfo().DeclaredEvents)
for (TypeInfo t = target.GetType().GetTypeInfo(); t != null && t != typeof(object).GetTypeInfo(); t = t.BaseType?.GetTypeInfo())
{
if (evt.AddMethod.IsPublic && !evt.AddMethod.IsStatic)
foreach (var evt in t.DeclaredEvents)
{
if (this.eventReceivers == null)
if (evt.AddMethod.IsPublic && !evt.AddMethod.IsStatic)
{
this.eventReceivers = new List<EventReceiver>();
}
if (this.eventReceivers == null)
{
this.eventReceivers = new List<EventReceiver>();
}

if (this.TraceSource.Switch.ShouldTrace(TraceEventType.Information))
{
this.TraceSource.TraceEvent(TraceEventType.Information, (int)TraceEvents.LocalEventListenerAdded, "Listening for events from {0}.{1} to raise notification.", target.GetType().FullName, evt.Name);
}
if (this.TraceSource.Switch.ShouldTrace(TraceEventType.Information))
{
this.TraceSource.TraceEvent(TraceEventType.Information, (int)TraceEvents.LocalEventListenerAdded, "Listening for events from {0}.{1} to raise notification.", target.GetType().FullName, evt.Name);
}

this.eventReceivers.Add(new EventReceiver(this, target, evt, options));
this.eventReceivers.Add(new EventReceiver(this, target, evt, options));
}
}
}
}
Expand Down