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

Add support for using extension methods in bindings #931

Merged
merged 20 commits into from
Feb 23, 2021

Conversation

acizmarik
Copy link
Member

This PR allows using extension methods inside bindings. #903 should be merged first.

  • Supported types to search for extension methods are defined within TypeRegistry
  • Extension methods are searched only in case the basic method search fails
    • Regular methods are always preferred

Currently the only registered type to look for extension methods is System.Linq.Enumerable. This makes it easier to define lambdas when using LINQ.

@acizmarik acizmarik added this to the Version 3.0 milestone Jan 29, 2021
Base automatically changed from feature/lambdas-in-command-bindings to main February 10, 2021 17:31
@acizmarik acizmarik marked this pull request as ready for review February 12, 2021 11:39
Copy link
Member

@exyi exyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice :)

@acizmarik
Copy link
Member Author

To use custom extension methods you can:

  • Implement own IExtensionsProvider or
  • Extend the DefaultExtensionsProvider

This needs to be then registered using dependency injection. If user registers no implementation, DotVVM uses the DefaultExtensionsProvider that has already the Enumerable type registered

if (members.Length == 0)
{
// We did not find any match in regular methods => try extension methods
var extensions = extensionsProvider.GetExtensionMethods()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we cache this? I am afraid that this will browse the types and look for the methods every single time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should. The implementation now caches the result of the method call GetMethods(..) to avoid unnecessary reflection. Also the attribute ExtensionAttribute is now checked to ensure that we yield only extension methods

@quigamdev quigamdev merged commit 3280b1a into main Feb 23, 2021
@acizmarik acizmarik deleted the feature/lambdas-extension-methods branch February 23, 2021 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants