-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Supporting calling non-generic instance methods on generic types #9061
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
Comments
Thanks for contacting us, @julienGrd. |
@mkArtakMSFT This PR on the old repo would have fixed it. If I can get approval then I will reimplement it on this repo instead - but I don't want to do that if there is no chance of it being merged in... |
@mrpmorris I'd recommend holding off on reimplementing the PR. The reason is that, if we're only supporting calling methods on generic types if it's an instance method and the method itself has no generic args [1], then my gut sense is that it should be an extremely small change, on the order of a few lines of code. The previous PR involved some extensive refactoring which I would hope we can avoid. [1] I do agree this is probably the right restriction, since you probably don't want to let client-side code pass arbitrary type args. There would be security implications if it can force the server to load types (and execute static constructors, etc.) that you never expected it to load. |
@SteveSandersonMS Hi Steve. Most of the code is actually test classes (probably 2/3 or 3/4 of the remaining 500 lines). |
FYI - for a workaround, I have found that using a child component to handle the JS Interop works, then I just wired up some Action parameters so the child can notify the Generic parent when something happens. |
I am having a similar issue but not necessarily to a component. I want to basically process JS promises back in C# by passing an instance method to JS to call when a promise succeeds (kind of a promise handler) which would populate a TaskCompletionSource object and return it to C#. |
JS invokeMethod to .NET GenericClass.Method() cause TargetException: Object does not match target type, even the parameter is not generic/T. |
@pranavkm You're already doing/done this, right? Do you want to self-assign and perhaps close as done? |
Merged as part of dotnet/extensions#2342. I'll keep this open to add a couple of E2E tests with Blazor. |
…ns#2342) * Add support for JSInvokable methods on generic types Prior to this change, DotNetDispatcher cached the MethodInfo on the generic type definition. Using this would have required MethodInfo.MakeGenericMethod before the method was invoked. We could separately cache the result of this to avoid the reflection cost per invocation. Alternatively we could cache static and non-static MethodInfo instances separately which is what this change attempts to do. The big difference in the outcome is that this requires instance (non-static) JSInvokable methods to be only unique named within the type hierarchy as opposed to across all static and instance JSInvokable methods in an assembly. Fixes dotnet/extensions#1360 Fixes #9061 \n\nCommit migrated from dotnet/extensions@659b604
…nsdotnet/extensionsdotnet/extensionsdotnet/extensions#2342) * Add support for JSInvokable methods on generic types Prior to this change, DotNetDispatcher cached the MethodInfo on the generic type definition. Using this would have required MethodInfo.MakeGenericMethod before the method was invoked. We could separately cache the result of this to avoid the reflection cost per invocation. Alternatively we could cache static and non-static MethodInfo instances separately which is what this change attempts to do. The big difference in the outcome is that this requires instance (non-static) JSInvokable methods to be only unique named within the type hierarchy as opposed to across all static and instance JSInvokable methods in an assembly. Fixes dotnet/extensions#1360 Fixes #9061 \n\nCommit migrated from https://github.com/dotnet/extensions/commit/dotnet/extensions@dotnet/extensions@dotnet/extensions@659b604fb2e595d48a931a7ed831f6c38f035382 \n\nCommit migrated from https://github.com/dotnet/extensions/commit/dotnet/extensions@dotnet/extensions@aaf9159b65e6fd911ffc63514bc3b179d4122d08 \n\nCommit migrated from https://github.com/dotnet/extensions/commit/dotnet/extensions@4f70b0248ef2ba6547cbe93bdea8169c2b826898 \n\nCommit migrated from dotnet/extensions@92c60ff
…ns#2342) * Add support for JSInvokable methods on generic types Prior to this change, DotNetDispatcher cached the MethodInfo on the generic type definition. Using this would have required MethodInfo.MakeGenericMethod before the method was invoked. We could separately cache the result of this to avoid the reflection cost per invocation. Alternatively we could cache static and non-static MethodInfo instances separately which is what this change attempts to do. The big difference in the outcome is that this requires instance (non-static) JSInvokable methods to be only unique named within the type hierarchy as opposed to across all static and instance JSInvokable methods in an assembly. Fixes dotnet/extensions#1360 Fixes #9061 \n\nCommit migrated from dotnet/extensions@659b604
Describe the bug
I have a generic component wich need some Js interaction. My component is an autocomplete wich listen on the keypress event to launch research. Whe i try invoke c# method from Js, I have this following Js error
"Error: System.InvalidOperationException: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.
"
To Reproduce
create a generic component, which contain an input type text
register js on render
Expected behavior
Have a way to call C# method without error
Additional context
My first idea was to use the key-* event of razor, but i was unable to have a satisfaying behavior with ths events (unable to retrieve the current value of my input, binding seem only fire when control lost focus), so i decided to switch on js.
If you have another solution, i take it ;-)
The text was updated successfully, but these errors were encountered: