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

Protected/private property/method support #76

Open
vflame opened this issue Jun 11, 2023 · 1 comment
Open

Protected/private property/method support #76

vflame opened this issue Jun 11, 2023 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@vflame
Copy link

vflame commented Jun 11, 2023

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

@koenbeuk
Copy link
Owner

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.

@koenbeuk koenbeuk added enhancement New feature or request help wanted Extra attention is needed labels Jun 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants