From 7a763fc4f6d1889a54ac9aa0aef00bed392d0baf Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Fri, 17 Aug 2018 04:35:24 -0700 Subject: [PATCH] Scan only declared methods for JS interop (#1320) * Scan only declared methods for JS interop * Add DotNetDispatcher test for JS invokable method on base class --- src/Microsoft.JSInterop/DotNetDispatcher.cs | 6 +++- .../DotNetDispatcherTest.cs | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.JSInterop/DotNetDispatcher.cs b/src/Microsoft.JSInterop/DotNetDispatcher.cs index 5612d9edcc..0f346bbfdd 100644 --- a/src/Microsoft.JSInterop/DotNetDispatcher.cs +++ b/src/Microsoft.JSInterop/DotNetDispatcher.cs @@ -240,7 +240,11 @@ namespace Microsoft.JSInterop var result = new Dictionary(); var invokableMethods = GetRequiredLoadedAssembly(assemblyName) .GetExportedTypes() - .SelectMany(type => type.GetMethods()) + .SelectMany(type => type.GetMethods( + BindingFlags.Public | + BindingFlags.DeclaredOnly | + BindingFlags.Instance | + BindingFlags.Static)) .Where(method => method.IsDefined(typeof(JSInvokableAttribute), inherit: false)); foreach (var method in invokableMethods) { diff --git a/test/Microsoft.JSInterop.Test/DotNetDispatcherTest.cs b/test/Microsoft.JSInterop.Test/DotNetDispatcherTest.cs index 315f26d832..eec537f987 100644 --- a/test/Microsoft.JSInterop.Test/DotNetDispatcherTest.cs +++ b/test/Microsoft.JSInterop.Test/DotNetDispatcherTest.cs @@ -155,6 +155,21 @@ namespace Microsoft.JSInterop.Test Assert.True(targetInstance.DidInvokeMyInvocableInstanceVoid); }); + [Fact] + public Task CanInvokeBaseInstanceVoidMethod() => WithJSRuntime(jsRuntime => + { + // Arrange: Track some instance + var targetInstance = new DerivedClass(); + jsRuntime.Invoke("unimportant", new DotNetObjectRef(targetInstance)); + + // Act + var resultJson = DotNetDispatcher.Invoke(null, "BaseClassInvokableInstanceVoid", 1, null); + + // Assert + Assert.Null(resultJson); + Assert.True(targetInstance.DidInvokeMyBaseClassInvocableInstanceVoid); + }); + [Fact] public Task CannotUseDotNetObjectRefAfterDisposal() => WithJSRuntime(jsRuntime => { @@ -376,6 +391,21 @@ namespace Microsoft.JSInterop.Test } } + public class BaseClass + { + public bool DidInvokeMyBaseClassInvocableInstanceVoid; + + [JSInvokable] + public void BaseClassInvokableInstanceVoid() + { + DidInvokeMyBaseClassInvocableInstanceVoid = true; + } + } + + public class DerivedClass : BaseClass + { + } + public class TestDTO { public string StringVal { get; set; }