Skip to content

Protected/private property/method support #76

Open

Description

@vflame

Is supporting the use of protected/private methods/members in a projected expression something that can be added?

Activity

  1. koenbeuk commented on Jun 11, 2023

    @koenbeuk
    Collaborator

    Expression trees are generated for projectable methods as part of an external class. A projectable expression is simply a rewrite of the original implementation. If this refers to a private member then that is not accessible from the external class. There are 2 workarounds possible:

    1. We generate expressions as part of a partial class. e.g., when you declare the container class of your projectables as Partial, we can generate the projectable methods within the same class and hence those generated projectable methods will have access to the internals of that class.

    2. We rewrite the generated expression to basically inline the logic of the internal member that it's referring to, e.g.

    class Foo {
      [Projectable] private int Sum(int a, int b) => a + b;
      [Projectable] public int SumAll(int[] numbers) => numbers.Aggregate((next, total) => Sum(total, next), 0);
     }
     
     // Generated code:
    class Generated_SumAll {
      // Before
      public Expression<Func<int[], int>> Expression = (int[] numbers) => numbers.Aggregate((next, total) => Foo.Sum(total, next), 0)
    
      // After
      public Expression<Func<int[], int>> Expression = (int[] numbers) => numbers.Aggregate((next, total) => total +  next, 0);
    }

    The downside of the second approach is that it would not be able to generate code for projectables that are part of a type that lives in a different assembly as it would not have access to the syntax tree for that method.

    Either way, this could be implemented. Happy to accept a PR here.

  2. Xriuk commented on May 28, 2026

    @Xriuk

    As a workaround, methods and properties can be marked as internal and

    [assembly: InternalsVisibleTo("EntityFrameworkCore.Projectables.Generated")]

    can be added to AssemblyInfo.cs

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesthelp wantedExtra attention is needed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions