Skip to content

Doesn't seem to work with generic extension methods #21

Open

Description

@MuTsunTsai

In my project I have a generic interface implemented by a few entity classes, and a generic extension method for that interface. When I tried using Expressionify on that method, the generator does come up with a static expression, but the type of it contains the method's generic parameter which is undefined in this context and causes compiling error.

Is there a way to fix this? Thanks!

Activity

  1. MuTsunTsai commented on Nov 5, 2022

    @MuTsunTsai
    Author

    I think in order to support generic methods, the generator will also have to output a generic static method (that returns the lambda expression) instead of a static property. For a given set of generic parameter assignment, the expression visitor will invoke the method for the first time, and the returned lambda expression can then be cached in a dictionary mapping sets of generic parameter assignments to the returned expressions.

  2. jeffward01 commented on Dec 11, 2022

    @jeffward01

    Thats a very creative way of solving identifying the source expression types. What about if you simply generate the constraint types in the extension methods?

    I will test this with Expressionfy, but I think that this might be the outcome: EFNext/EntityFrameworkCore.Projectables#54 (comment)

  3. MuTsunTsai commented on Dec 12, 2022

    @MuTsunTsai
    Author

    Replacing with the contraint type might work in some cases, but I don't think it will work in general. For example, if the returned type of the expression also involves the generic parameter, such a replacement could make the result too general for whoever is using the expression.

  4. mariusGundersen commented on Dec 12, 2022

    @mariusGundersen
    Contributor

    Can you show an example of what you mean? How would it be too general?

  5. MuTsunTsai commented on Dec 14, 2022

    @MuTsunTsai
    Author

    @mariusGundersen Consider something as simple as

    public interface IProp<T> {
        T prop { get; set; }
    }
    
    public static T GetProp<T>(this IProp<T> object) => object.prop;

    In this case replaceing T with its constraint (none in this case, so it will simply be object) will result in the extension method returning just an object instead of a T, and thus runtime error will occur if other part of the expression tries to access anything specific about T on the returned result.

  6. mariusGundersen commented on Dec 14, 2022

    @mariusGundersen
    Contributor

    I am working on a version that supports generics. Can you try with pre-release version 6.7.0-generics.41?

  7. MuTsunTsai commented on Dec 20, 2022

    @MuTsunTsai
    Author

    @mariusGundersen Still not working. It still shows an error message saying that "cannot find the type or namespace of [name of the generic parameter]" (paraphrased). It seems that the generated .g.cs file is immediately deleted afterwards so I have no idea what code was being generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions