Support for projectable generic methods

This commit is contained in:
Koen Bekkenutte
2021-10-27 01:16:26 +08:00
parent 14a8f1ebd2
commit ede53de1c2
45 changed files with 373 additions and 96 deletions
@@ -15,21 +15,6 @@ namespace EntityFrameworkCore.Projectables.Generator
_semanticModel = semanticModel;
}
public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
{
var visitedNode = base.VisitIdentifierName(node);
var symbolInfo = _semanticModel.GetSymbolInfo(visitedNode);
if (symbolInfo.Symbol is not null)
{
return SyntaxFactory.IdentifierName(symbolInfo.Symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat))
.WithTriviaFrom(node);
}
return visitedNode;
}
public override SyntaxNode? VisitParameter(ParameterSyntax node)
{
var visitedNode = base.VisitParameter(node);
@@ -2,6 +2,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -28,6 +29,10 @@ namespace EntityFrameworkCore.Projectables.Generator
public ParameterListSyntax ParametersList { get; set; }
public TypeParameterListSyntax TypeParameterList { get; set; }
public IEnumerable<TypeParameterConstraintClauseSyntax> ConstraintClauses { get; set; }
public SyntaxNode Body { get; set; }
}
}
@@ -59,7 +59,8 @@ namespace EntityFrameworkCore.Projectables.Generator
ClassNamespace = memberSymbol.ContainingType.ContainingNamespace.IsGlobalNamespace ? null : memberSymbol.ContainingType.ContainingNamespace.ToDisplayString(),
MemberName = memberSymbol.Name,
NestedInClassNames = GetNestedInClassPath(memberSymbol.ContainingType),
ParametersList = SyntaxFactory.ParameterList()
ParametersList = SyntaxFactory.ParameterList(),
TypeParameterList = SyntaxFactory.TypeParameterList()
};
if (!memberDeclarationSyntax.Modifiers.Any(SyntaxKind.StaticKeyword))
@@ -78,7 +79,9 @@ namespace EntityFrameworkCore.Projectables.Generator
);
}
if (memberSymbol is IMethodSymbol methodSymbol && methodSymbol.IsExtensionMethod)
var methodSymbol = memberSymbol as IMethodSymbol;
if (methodSymbol is { IsExtensionMethod: true })
{
var targetTypeSymbol = methodSymbol.Parameters.First().Type;
descriptor.TargetClassNamespace = targetTypeSymbol.ContainingNamespace.IsGlobalNamespace ? null : targetTypeSymbol.ContainingNamespace.ToDisplayString();
@@ -107,6 +110,20 @@ namespace EntityFrameworkCore.Projectables.Generator
{
descriptor.ParametersList = descriptor.ParametersList.AddParameters(additionalParameter);
}
if (methodDeclarationSyntax.TypeParameterList is not null)
{
foreach (var additionalTypeParameter in ((TypeParameterListSyntax)declarationSyntaxRewriter.Visit(methodDeclarationSyntax.TypeParameterList)).Parameters)
{
descriptor.TypeParameterList = descriptor.TypeParameterList.AddParameters(additionalTypeParameter);
}
}
if (methodDeclarationSyntax.ConstraintClauses.Any())
{
descriptor.ConstraintClauses = methodDeclarationSyntax.ConstraintClauses
.Select(x => (TypeParameterConstraintClauseSyntax)declarationSyntaxRewriter.Visit(x));
}
}
else if (memberDeclarationSyntax is PropertyDeclarationSyntax propertyDeclarationSyntax)
{
@@ -72,11 +72,26 @@ namespace EntityFrameworkCore.Projectables.Generated
{{
public static class {generatedClassName}
{{
public static System.Linq.Expressions.Expression<System.Func<{lambdaTypeArguments.Arguments}, {projectable.ReturnTypeName}>> Expression =>
{projectable.ParametersList} => {projectable.Body};
public static System.Linq.Expressions.Expression<System.Func<{lambdaTypeArguments.Arguments}, {projectable.ReturnTypeName}>> Expression{(projectable.TypeParameterList.Parameters.Any() ? projectable.TypeParameterList.ToString() : string.Empty)}()");
if (projectable.ConstraintClauses is not null)
{
foreach (var constraintClause in projectable.ConstraintClauses)
{
resultBuilder.Append($@"
{constraintClause}");
}
}
resultBuilder.Append($@"
{{
return {projectable.ParametersList} =>
{projectable.Body};
}}
}}
}}");
context.AddSource($"{generatedClassName}_Generated", SourceText.From(resultBuilder.ToString(), Encoding.UTF8));
}
}
@@ -12,31 +12,37 @@ namespace EntityFrameworkCore.Projectables.Services
{
public sealed class ProjectionExpressionResolver : IProjectionExpressionResolver
{
readonly ConcurrentDictionary<string, LambdaExpression> _lookupCache = new();
public LambdaExpression FindGeneratedExpression(MemberInfo projectableMemberInfo)
{
var reflectedType = projectableMemberInfo.ReflectedType ?? throw new InvalidOperationException("Expected a valid type here");
var generatedContainingTypeName = ProjectionExpressionClassNameGenerator.GenerateFullName(reflectedType.Namespace, reflectedType.GetNestedTypePath().Select(x => x.Name), projectableMemberInfo.Name);
return _lookupCache.GetOrAdd(generatedContainingTypeName, _ => {
var expressionFactoryMethod = reflectedType.Assembly
.GetTypes()
.Where(x => x.FullName == generatedContainingTypeName)
.SelectMany(x => x.GetMethods())
.FirstOrDefault();
var genericArguments = projectableMemberInfo switch {
MethodInfo methodInfo => methodInfo.GetGenericArguments(),
_ => null
};
if (expressionFactoryMethod is null)
{
throw new InvalidOperationException("Unable to resolve generated expression") {
Data = {
["GeneratedContainingTypeName"] = generatedContainingTypeName
}
};
}
var expressionFactoryMethod = reflectedType.Assembly
.GetTypes()
.Where(x => x.FullName == generatedContainingTypeName)
.SelectMany(x => x.GetMethods())
.FirstOrDefault();
return expressionFactoryMethod.Invoke(null, null) as LambdaExpression ?? throw new InvalidOperationException("Expected lambda");
});
if (expressionFactoryMethod is null)
{
throw new InvalidOperationException("Unable to resolve generated expression") {
Data = {
["GeneratedContainingTypeName"] = generatedContainingTypeName
}
};
}
if (genericArguments is { Length: > 0 } )
{
expressionFactoryMethod = expressionFactoryMethod.MakeGenericMethod(genericArguments);
}
return expressionFactoryMethod.Invoke(null, null) as LambdaExpression ?? throw new InvalidOperationException("Expected lambda");
}
}
}