Skip to content

Add support for interfaces in generic methods#57

Merged
koenbeuk merged 2 commits intoEFNext:masterfrom
rhodon-jargon:interface-support
Jan 15, 2023
Merged

Add support for interfaces in generic methods#57
koenbeuk merged 2 commits intoEFNext:masterfrom
rhodon-jargon:interface-support

Conversation

@rhodon-jargon
Copy link

@rhodon-jargon rhodon-jargon commented Jan 11, 2023

This way, an expression in a generic method that references an interface member can be translated as long as the runtime generic argument is a concrete type. For example:

public static IQueryable<int> SelectComputedProperty<TConcrete>(this IQueryable<TConcrete> concretes)
    where TConcrete : InheritedModelTests.IBase
    => concretes.Select(x => x.ComputedProperty);

Here, when SelectComputedProperty is called like SelectComputedProperty<InheritedModelTests.Concrete> (where the argument can be implicit like in InheritedModelTests.cs), the correct implementation method can be found in Concrete.
However, when it is called like SelectComputedProperty<InheritedModelTests.IBase>, the concrete implementation is unknown and the method can't be translated.

I also changed overriding member logic (from #52) to use MethodInfo.GetBaseDefinition() instead of the name and signature of the method.

I changed some tests, because there were two duplicate tests in InheritedModelTests.cs. I assumed these modifications were the original intention.

Possible improvements:

  • Additions to documentation
  • Maybe some extra tests
  • Helpful user feedback, like throwing an error when the generic argument is an interface (and thus can't be translated)

Thanks for creating and maintaining this package!

Rhodon added 2 commits January 11, 2023 14:08
Change overriding member logic to use MethodInfo.GetBaseDefinition()
@koenbeuk
Copy link
Collaborator

Awesome! I'll review this tomorrow.

@koenbeuk koenbeuk merged commit 1771c41 into EFNext:master Jan 15, 2023
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.

2 participants